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
+12.3k Golang : Get absolute path to binary for os.Exec function with exec.LookPath
+8.2k Golang : How to check if input string is a word?
+4.6k Nginx and PageSpeed build from source CentOS example
+45.9k Golang : Encode image to base64 example
+22k Golang : Read directory content with filepath.Walk()
+15.7k Golang : Generate universally unique identifier(UUID) example
+9.1k Golang : Scramble and unscramble text message by randomly replacing words
+21.5k Golang : How to reverse slice or array elements order
+23.2k Find and replace a character in a string in Go
+8.2k Golang : Ackermann function example
+8.5k Golang : Gorilla web tool kit schema example
+8k Golang : Check if integer is power of four example