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
+31k Golang : Get local IP and MAC address
+9.4k Golang : Populate slice with sequential integers example
+15.6k Golang : Read a file line by line
+5.8k Golang : Build new URL for named or registered route with Gorilla webtoolkit example
+9.9k Golang : How to tokenize source code with text/scanner package?
+16.8k Golang : How to tell if a file is compressed either gzip or zip ?
+5.5k Golang : Fix opencv.LoadHaarClassifierCascade The node does not represent a user object error
+10.7k Golang : Generate random elements without repetition or duplicate
+15.2k Golang : Validate hostname
+9.6k Golang : Check if user agent is a robot or crawler example
+11k Golang : Characters limiter example
+12.8k Golang : Convert(cast) int to int64