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

1
2
3
if resp.StatusCode == 200 {
    // do something if the status code is 200
}

You can write

1
2
3
if resp.StatusCode == http.StatusOK {
    // do something if the status code is 200
}

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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")
	}
}

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
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
	}
}
$ go run example.go

Not Found

Super convenient.

References