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
+6.3k Golang : How to determine if request or crawl is from Google robots
+14.3k Golang : Convert(cast) int to float example
+16.1k Golang : Find out mime type from bytes in buffer
+14k Golang : Convert IP version 6 address to integer or decimal number
+20.9k Golang : Sort and reverse sort a slice of strings
+12.3k Golang : Forwarding a local port to a remote server example
+37.2k Upload multiple files with Go
+8.5k Golang : How to join strings?
+5k Golang : Print instead of building pyramids
+7.6k Golang : Get today's weekday name and calculate target day distance example
+13.8k Golang: Pad right or print ending(suffix) zero or spaces in fmt.Printf example
+5.9k Golang : Get missing location after unmarshal binary and gob decode time.