Golang : How to return HTTP status code?
Was helping out a friend today on her work and one of the problems that she encountered during coding was on returning HTTP status code. Golang has a builtin constants of HTTP status codes ( http://golang.org/pkg/net/http/#pkg-constants ) that developer can use to return the status back to the client.
Here is an example usage :
package main
import (
"net/http"
)
func returnCode200(w http.ResponseWriter, r *http.Request) {
// see http://golang.org/pkg/net/http/#pkg-constants
w.WriteHeader(http.StatusOK)
w.Write([]byte("☄ HTTP status code returned!"))
}
func returnCode500(w http.ResponseWriter, r *http.Request) {
// see http://golang.org/pkg/net/http/#pkg-constants
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("☄ HTTP status code returned!"))
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", returnCode200)
mux.HandleFunc("/fivehundred", returnCode500)
http.ListenAndServe(":8080", mux)
}
Sample tests with curl :
curl -I socketloop.com:8080
HTTP/1.1 200 OK
Date: Tue, 12 May 2015 07:11:40 GMT
Content-Length: 26
Content-Type: text/plain; charset=utf-8
curl -I socketloop.com:8080/fivehundred
HTTP/1.1 500 Internal Server Error
Date: Tue, 12 May 2015 07:13:06 GMT
Content-Length: 26
Content-Type: text/plain; charset=utf-8
References :
StatusContinue = 100
StatusSwitchingProtocols = 101
StatusOK = 200
StatusCreated = 201
StatusAccepted = 202
StatusNonAuthoritativeInfo = 203
StatusNoContent = 204
StatusResetContent = 205
StatusPartialContent = 206
StatusMultipleChoices = 300
StatusMovedPermanently = 301
StatusFound = 302
StatusSeeOther = 303
StatusNotModified = 304
StatusUseProxy = 305
StatusTemporaryRedirect = 307
StatusBadRequest = 400
StatusUnauthorized = 401
StatusPaymentRequired = 402
StatusForbidden = 403
StatusNotFound = 404
StatusMethodNotAllowed = 405
StatusNotAcceptable = 406
StatusProxyAuthRequired = 407
StatusRequestTimeout = 408
StatusConflict = 409
StatusGone = 410
StatusLengthRequired = 411
StatusPreconditionFailed = 412
StatusRequestEntityTooLarge = 413
StatusRequestURITooLong = 414
StatusUnsupportedMediaType = 415
StatusRequestedRangeNotSatisfiable = 416
StatusExpectationFailed = 417
StatusTeapot = 418
StatusInternalServerError = 500
StatusNotImplemented = 501
StatusBadGateway = 502
StatusServiceUnavailable = 503
StatusGatewayTimeout = 504
StatusHTTPVersionNotSupported = 505
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+47.6k Golang : How to convert JSON string to map and slice
+18.7k Golang : Read input from console line
+18.7k Golang : Check whether a network interface is up on your machine
+8k Golang : Qt splash screen with delay example
+12.6k Golang : Convert int(year) to time.Time type
+4.8k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+9.8k Golang : Find and replace data in all files recursively
+11.2k Golang : Generate DSA private, public key and PEM files example
+8.8k Golang : Serving HTTP and Websocket from different ports in a program example
+14.1k Golang : How to shuffle elements in array or slice?
+11.2k Golang : Handle API query by curl with Gorilla Queries example
+19.3k Golang : Close channel after ticker stopped example