Get startedGet started for free

Headers and status codes

1. Headers and status codes

So far, we have only sent a request message to a server and processed the response. But what if we want to give the server extra instructions? Or check that the server properly handled our request? That is where headers and status-codes come in.

2. Request and response message anatomy

Examining a GET request and response message, we see both messages are very similar in structure and can both be split into three distinct parts.

3. The start-line

First, we have the start-line. In request messages, this is referred to as the request-line. It contains the request type such as `GET` or `POST`, along with the path where the message should be delivered to. In response messages, the start line is called the status-line and contains a three-digit numerical status-code and a status message.

4. Status codes

There are over 70 status codes in total, grouped into five categories as shown here. The most important ones to remember are '200 OK', which indicates the server has correctly processed the request, '404 Not Found', to indicate that the resource we are requesting doesn't exist, and `500 Internal Server Error`, which means an error occurred on the server.

5. Headers

Headers contain information that describe the message or data being sent or received, such as the type of content we are sending or the date the requested resource was last modified. Headers are always formatted as key-value pairs separated by a colon. Each header starts with a case-insensitive key, followed by a colon, and then the value of that header.

6. Example: Content negotiation with headers

In order to effectively communicate, client and server use message headers to agree on the language they are using to exchange information. This is called content negotiation. Here for example, the client sends the `accept` header to inform the server it can accept a response in JSON format. When the server responds, it includes the content-type header to let the client know what format it responded with.

7. Headers with requests

The Python requests package allows us to add and read headers. Each request method like requests.get or requests.post accepts an additional headers parameter with key-value pairs in the form of a dictionary. Using this parameter, we can add as many headers as we want to our request. The response object has a headers attribute, which is a dictionary containing one key-value pair for every header received back in the response. We can access individual response headers by subsetting the dictionary using square brackets, or by using the .get() method on the dictionary.

8. Status codes with requests

Similar to working with headers, the requests package also simplifies how we get the status-code from a response. Each response object has a status-code attribute which contains the numeric value of the status-code. Remembering all the numeric values of the status codes by heart isn't easy however, that's why requests comes with an easy lookup object built-in. By chaining the status-message to the requests.codes lookup object we can easily find any status code, without the need to know the code. Like here where we use the lookup object to find the status-code for Not Found.

9. Let's practice!

Now we have a better understanding of headers and status-codes, let's practice what we learned using the requests package!