<\/span><\/h2>\n\n\n\nLet’s see an example of sending http status code and JSON response body<\/p>\n\n\n\n
Below is the program for the same<\/p>\n\n\n\n
package main\n\nimport (\n\t\"encoding\/json\"\n\t\"log\"\n\t\"net\/http\"\n)\n\nfunc main() {\n\thandler := http.HandlerFunc(handleRequest)\n\thttp.Handle(\"\/example\", handler)\n\thttp.ListenAndServe(\":8080\", nil)\n}\n\nfunc handleRequest(w http.ResponseWriter, r *http.Request) {\n\tw.WriteHeader(http.StatusCreated)\n\tw.Header().Set(\"Content-Type\", \"application\/json\")\n\tresp := make(map[string]string)\n\tresp[\"message\"] = \"Status Created\"\n\tjsonResp, err := json.Marshal(resp)\n\tif err != nil {\n\t\tlog.Fatalf(\"Error happened in JSON marshal. Err: %s\", err)\n\t}\n\tw.Write(jsonResp)\n\treturn\n}<\/code><\/pre>\n\n\n\nIn the above program, this is how we create a JSON response. We use the json.Marshal<\/strong> function to convert the map[string]string<\/strong> into json bytes.<\/p>\n\n\n\nresp := make(map[string]string)\nresp[\"message\"] = \"Status Created\"\njsonResp, err := json.Marshal(resp)\nif err != nil {\n\tlog.Fatalf(\"Error happened in JSON marshal. Err: %s\", err)\n}\nw.Write(jsonResp)<\/code><\/pre>\n\n\n\nIt then uses the Write<\/strong> function to return the JSON response body. The above code returns the below JSON response body back in response<\/p>\n\n\n\n{\"message\":\"Status Created\"}<\/code><\/pre>\n\n\n\nAlso, we are using the WriteHeader<\/strong> function to specify the 201 http status code.<\/p>\n\n\n\nRun the above program. It will start a server on 8080 port on your local machine. Now make the below curl call to the server<\/p>\n\n\n\n
curl -v -X POST http:\/\/localhost:8080\/example<\/code><\/pre>\n\n\n\nBelow will be the output<\/p>\n\n\n\n
* Connected to localhost (::1) port 8080 (#0)\n> POST \/example HTTP\/1.1\n> Host: localhost:8080\n> User-Agent: curl\/7.54.0\n> Accept: *\/*\n> \n< HTTP\/1.1 201 Created\n< Date: Sat, 10 Jul 2021 10:40:33 GMT\n< Content-Length: 28\n< Content-Type: text\/plain; charset=utf-8\n< \n* Connection #0 to host localhost left intact\n{\"message\":\"Status Created\"}<\/code><\/pre>\n\n\n\nAs you can see from the output, it will correctly return the 201<\/strong> status code along with the JSON body.<\/p>\n\n\n\nAlso, check out our Golang advance tutorial Series - Golang Advance Tutorial<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"Table of Contents OverviewExample Overview Write method of the ResponseWriter interface in net\/http package can be used to set the JSON body in an HTTP response In GO a response is represented…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[3,4],"class_list":["post-5930","post","type-post","status-publish","format-standard","hentry","category-tech","tag-go","tag-golang"],"yoast_head":"\n
Return JSON body in HTTP response in Go (Golang) - Welcome To Golang By Example<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n