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
+10.4k Golang : Underscore string example
+33.5k Golang : convert(cast) bytes to string
+9.1k Golang : Launch Mac OS X Preview (or other OS) application from your program example
+12.5k Swift : Convert (cast) Int or int32 value to CGFloat
+16.1k Golang : Convert slice to array
+3.9k Javascript : Empty an array example
+9k Golang : Create and shuffle deck of cards example
+4.5k JavaScript: Add marker function on Google Map
+32.8k Delete a directory in Go
+12.4k Golang : zlib compress file example
+3.1k Golang : Fix go-cron set time not working issue
+8.7k Golang : Populate or initialize struct with values example