Changing json encoding of response 73/34373/2
authorKiran Kamineni <kiran.k.kamineni@intel.com>
Wed, 7 Mar 2018 00:18:49 +0000 (16:18 -0800)
committerGirish Havaldar <hg0071052@techmahindra.com>
Wed, 7 Mar 2018 04:01:39 +0000 (04:01 +0000)
Updating the json encoding to use Marshal
This is to allow us to set the right return Header type
and also return the right status codes

Issue-ID: AAF-160
Change-Id: Ib260e5b8306b16069c57f6b83efcf401747ff2b6
Signed-off-by: Kiran Kamineni <kiran.k.kamineni@intel.com>
sms-service/src/sms/handler/handler.go

index 496b782..4456806 100644 (file)
@@ -50,14 +50,16 @@ func (h handler) createSecretDomainHandler(w http.ResponseWriter, r *http.Reques
                return
        }
 
-       err = json.NewEncoder(w).Encode(dom)
+       jdata, err := json.Marshal(dom)
        if err != nil {
                smslogger.WriteError(err.Error())
                http.Error(w, err.Error(), http.StatusInternalServerError)
                return
        }
 
+       w.Header().Set("Content-Type", "application/json")
        w.WriteHeader(http.StatusCreated)
+       w.Write(jdata)
 }
 
 // deleteSecretDomainHandler deletes a secret domain with the name provided
@@ -113,12 +115,15 @@ func (h handler) getSecretHandler(w http.ResponseWriter, r *http.Request) {
                return
        }
 
-       err = json.NewEncoder(w).Encode(sec)
+       jdata, err := json.Marshal(sec)
        if err != nil {
                smslogger.WriteError(err.Error())
                http.Error(w, err.Error(), http.StatusInternalServerError)
                return
        }
+
+       w.Header().Set("Content-Type", "application/json")
+       w.Write(jdata)
 }
 
 // listSecretHandler handles listing all secrets under a particular domain name
@@ -133,12 +138,15 @@ func (h handler) listSecretHandler(w http.ResponseWriter, r *http.Request) {
                return
        }
 
-       err = json.NewEncoder(w).Encode(sec)
+       jdata, err := json.Marshal(sec)
        if err != nil {
                smslogger.WriteError(err.Error())
                http.Error(w, err.Error(), http.StatusInternalServerError)
                return
        }
+
+       w.Header().Set("Content-Type", "application/json")
+       w.Write(jdata)
 }
 
 // deleteSecretHandler handles deleting a secret by given domain name and secret name
@@ -170,12 +178,15 @@ func (h handler) statusHandler(w http.ResponseWriter, r *http.Request) {
        }
 
        status := backendStatus{Seal: s}
-       err = json.NewEncoder(w).Encode(status)
+       jdata, err := json.Marshal(status)
        if err != nil {
                smslogger.WriteError(err.Error())
                http.Error(w, err.Error(), http.StatusInternalServerError)
                return
        }
+
+       w.Header().Set("Content-Type", "application/json")
+       w.Write(jdata)
 }
 
 // loginHandler handles login via password and username