DEV Community

Nelson Figueroa
Nelson Figueroa

Posted on • Originally published at nelson.cloud on

Validate HTTP Status Codes in Go Using Built-in Constants

Constants to Use When Checking for HTTP Status Codes

Go has useful constants in the net/http package that can make your code more readable when checking for status codes in responses.

For example, instead of writing something like

if resp.StatusCode == 200 {
    // do something if the status code is 200
}
Enter fullscreen mode Exit fullscreen mode

You can write

if resp.StatusCode == http.StatusOK {
    // do something if the status code is 200
}
Enter fullscreen mode Exit fullscreen mode

Unfortunately the the link doesn’t show any any full, working examples. So here’s an example covering some of the more common http status codes that you can use and modify as needed.

package main

import (
    "fmt"
    "log"
    "net/http"
)

func main() {
    resp, err := http.Get("https://example.com")
    if err != nil {
        log.Fatalf("error: %v", err)
    }

    defer resp.Body.Close()

    // Status code 200
    if resp.StatusCode == http.StatusOK {
        fmt.Println("ok!")
    }

    // Status code 301
    if resp.StatusCode == http.StatusMovedPermanently {
        fmt.Println("moved permanently")
    }

    // Status code 403
    if resp.StatusCode == http.StatusForbidden {
        fmt.Println("forbidden")
    }

    // Status code 404
    if resp.StatusCode == http.StatusNotFound {
        fmt.Println("not found")
    }

    // Status code 429
    if resp.StatusCode == http.StatusTooManyRequests {
        fmt.Println("too many requests")
    }

    // Status code 500
    if resp.StatusCode == http.StatusInternalServerError {
        fmt.Println("internal server error")
    }

    // Status code 502
    if resp.StatusCode == http.StatusBadGateway {
        fmt.Println("bad gateway")
    }
}
Enter fullscreen mode Exit fullscreen mode

Turning Http Status Codes into Messsages with Statustext()

There’s also a function http.StatusText() that allows you to pass in status codes and get a message for logging purposes or displaying to users. You can see all the responses in the source code here, it’s a bunch of case statements.

For example, lets say we want to display a message if we don’t get a 200 status code. Instead of writing code to print out a different message depending on the status code value, we can use the http.StatusText() function:

package main

import (
    "fmt"
    "log"
    "net/http"
)

func main() {
    resp, err := http.Get("https://example.com/404") // this URL returns a 404 status code
    if err != nil {
        log.Fatalf("error: %v", err)
    }

    defer resp.Body.Close()

    if resp.StatusCode != http.StatusOK {
        // if it's not 200, print out a message depending on the status code
        fmt.Println(http.StatusText(resp.StatusCode)) // http.StatusText() is called here
    }
}
Enter fullscreen mode Exit fullscreen mode
$ go run example.go

Not Found
Enter fullscreen mode Exit fullscreen mode

Super convenient.

References

Top comments (0)