<\/span><\/h2>\n\n\n\nBelow is the program for the same<\/p>\n\n\n\n
package main\n\nimport (\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.StatusBadRequest)\n\treturn\n}<\/code><\/pre>\n\n\n\nHere we are using the WriteHeader<\/strong> function to specify the 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 400 Bad Request\n< Date: Sat, 10 Jul 2021 05:50:32 GMT\n< Content-Length: 0\n< \n* Connection #0 to host localhost left intact<\/code><\/pre>\n\n\n\nAs you can see from the output, it will correctly return the 400 status code. If we also want to return the JSON error body, then below is the code 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.StatusBadRequest)\n\tw.Header().Set(\"Content-Type\", \"application\/json\")\n\tresp := make(map[string]string)\n\tresp[\"message\"] = \"Bad Request\"\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\nThe above code returns the below JSON request body back in response<\/p>\n\n\n\n
{\"message\":\"Bad Request\"}<\/code><\/pre>\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 400 Bad Request\n< Date: Sat, 10 Jul 2021 05:58:42 GMT\n< Content-Length: 25\n< Content-Type: text\/plain; charset=utf-8\n< \n* Connection #0 to host localhost left intact\n{\"message\":\"Bad Request\"}<\/code><\/pre>\n\n\n\nAs you can see from the output, it correctly returns the 400 status code along with the body.<\/p>\n\n\n\n
You can also directly pass 400 to the WriteHeader function to send the 400 response.<\/p>\n\n\n\n
w.WriteHeader(400)<\/code><\/pre>\n\n\n\nThis also works correctly. Try it out.<\/p>\n\n\n\n
Also, check out our Golang advance tutorial Series - Golang Advance Tutorial<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"Table of Contents OverviewProgram Overview net\/http package of golang provides the status code constants which could be used to return different status codes- https:\/\/golang.org\/src\/net\/http\/status.go The same can also be used to return…<\/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":[331,3,4],"class_list":["post-5872","post","type-post","status-publish","format-standard","hentry","category-tech","tag-331","tag-go","tag-golang"],"yoast_head":"\n
Return 400 (Bad Request) Status Code 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