Golang : Intercept and compare HTTP response code example
Problem :
You want to query a website and intercept the response code. You also want to compare the response code to a built in HTTP response codes in net/http
package. How to do that?
Solution :
Use the http.Head()
function to query a website. The return codes can be compared with the ==
operator.
For example :
package main
import (
"fmt"
"net/http"
"os"
)
func main() {
url := "https://www.socketloop.com"
response, err := http.Head(url)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if response.StatusCode != http.StatusOK { // <------- here !
fmt.Printf("%s returns non-200 status code. \n", url)
fmt.Printf("The reply : %v \n", response.StatusCode)
} else {
fmt.Println(response)
}
}
Happy coding!
References :
StatusContinue = 100 // access with http.StatusContinue
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
https://www.socketloop.com/tutorials/golang-how-to-return-http-status-code
See also : Golang : How to return HTTP status code?
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
+6k Golang : Scan forex opportunities by Bollinger bands
+16.5k Golang : read gzipped http response
+9.4k Golang : Sort and reverse sort a slice of floats
+8.7k Golang : Get SPF and DMARC from email headers to fight spam
+12k Golang : calculate elapsed run time
+7.5k Golang : Lock executable to a specific machine with unique hash of the machine
+12.9k Golang : Convert(cast) int to int64
+30.1k Golang : How to redirect to new page with net/http?
+38.8k Golang : How to iterate over a []string(array)
+6.6k Swift : substringWithRange() function example
+46k Golang : Marshal and unmarshal json.RawMessage struct example
+11.8k Golang : Pagination with go-paginator configuration example