https:\/\/golang.org\/src\/net\/http\/status.go<\/a><\/p>\n\n\n\nThe same can also be used to return the 401 (Unauthorized) HTTP status code. The HTTP 401 status code is defined by the below constant<\/p>\n\n\n\n
http.StatusUnauthorized<\/code><\/pre>\n\n\n\nIn this article, we will also see how to return a JSON body along with the 401 (Unauthorized) Status Code<\/p>\n\n\n\n
<\/span>Program<\/strong><\/span><\/h2>\n\n\n\nBelow 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.StatusUnauthorized)\n\tw.Header().Set(\"Content-Type\", \"application\/json\")\n\tresp := make(map[string]string)\n\tresp[\"message\"] = \"Unauthorized\"\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\nHere we are using the WriteHeader<\/strong> function to specify the 401 http status code and uses the Write<\/strong> function to also return the response bodyThe above code returns the below JSON request body back in response<\/p>\n\n\n\n{\"message\":\"Unauthorized\"}<\/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 401 Unauthorized\n< Date: Sat, 10 Jul 2021 08:19:52 GMT\n< Content-Length: 26\n< Content-Type: text\/plain; charset=utf-8\n< \n* Connection #0 to host localhost left intact\n{\"message\":\"Unauthorized\"}<\/code><\/pre>\n\n\n\nAs you can see from the output, it will correctly return the 401<\/strong> status code along with the body.<\/p>\n\n\n\nYou can also directly pass 401 to the WriteHeader function to send the 401 response.<\/p>\n\n\n\n
w.WriteHeader(401)<\/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":[332,3,4],"class_list":["post-5888","post","type-post","status-publish","format-standard","hentry","category-tech","tag-332","tag-go","tag-golang"],"yoast_head":"\n
Return 401 (UnAuthorized) 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