An intro to HTTP status codes and HTTP methods

NJ Pearman
EPFL Extension School
7 min readJul 20, 2018
We see 404 all the time while browsing the web. This is because it’s one of the many HTTP status codes.

HTTP, or Hypertext Transfer Protocol, is a fundamental for understanding how the Internet works and for aspirations to become a web developer. It defines how messages are formatted and sent over the Internet.

In other words, when we type an address, or URL, into a web browser, it is HTTP that that is used to send the request to a web server and to format the response message that gets sent back— most likely a web page! For example, https://exts.epfl.ch is the address of the EPFL Extension School.

When the browser sends the request to this URL, we expect the web page back as the response to the request. The web page is part of the response called the HTTP response body. It is the main thing that we are interested in when we are using a web browser and our browsers will handle that response body by parsing and rendering the HTML that it contains.

But there are other parts to HTTP in addition to the response body, which are equally important when it comes to working as a web developer. We need to interpret the HTTP requests that are received on a web server in full, and respond appropriately by creating a complete HTTP response.

So let’s look at HTTP methods and HTTP status codes. Both of these might be unfamiliar terms but what they represent will probably be familiar to anyone who has used the Internet a lot. If you’ve seen numbers or codes like 404 or 500 displayed when a website seems broken, then you’ve seen two different HTTP status codes. Let’s consider them first.

HTTP status codes

HTTP status codes are sent in an HTTP response along with the response body. A status code is an indicator of what the web server has managed to do with a request. It is a three-digit number, and 404 or 500 are two of the possible status codes that a web server might respond with. They indicate some sort of error though, so let’s start off with the “happy path” of successfully loading the web page that we requested.

When we see a new page load successfully in a browser, the status code that accompanies it is 200. This simply means OK, and indicates that the request we sent makes sense, that the server has no problems with finding that page, and that the page matching that request is containined in the response body. As 200 means OK or success, then the vast majority of pages that we load through our browser will be accompanied by the status code 200. We can even see this explcitly by opening the Developer Tools in the Firefox browser before loading a page.

Here the HTTP status code that was sent in the response has been highlighted, below the requested address and HTTP method, which we’ll learn about later.

An HTTP status code can indicate something besides “success” to the browser though, and can be accompanied by other information in addition to or instead of a response body. Here are some common status codes that you might recognise:

  • 404 Not found. This indicates that the web server can’t find a web page or any other resource that matches the requested URL. This can happen because of a typo in a web address or because a page has been deleted.
  • 500 Internal server error. Basically, the web server has encountered an error when trying to respond. This happens when the code running on the server is not working properly.
  • 400 Bad request. This means that something is badly formatted in the request that we’ve sent, perhaps an error in some URL parameters or we’ve tried to upload a file that is too large.
  • 401 Not authorized. This means we’re trying to access a web page without identifying ourselves. In other words, we need to log in.

There are many HTTP status codes, and each one indicates that the web server has treated our request in a particular way. Wikipedia is a good place to get a full list of HTTP status codes — check out status code 418 in particular as it’s a comical entry in the list!

Using HTTP status codes

These status codes are important to understand when doing web development because we, as web developers, need to make sure that the web apps that we build always respond to requests with the correct status codes. If the code we write doesn’t set the correct status code then the browser receiving the response could handle it incorrectly.

Luckily for us, when we use web frameworks to build web application–such as Ruby on Rails, which we teach in the EPFL Extension School Web Application Development program–the framework will automatically set the HTTP status code for the most common situations.

But it’s still important to know which HTTP status code is used for what situation. Being able to manually set status codes helps create a complete picture of how HTTP works, and how browsers and web servers communicate.

There are a huge number of scenarios and status codes to explore. So be curious about what scenario a particular HTTP status code is used for.

One factor that can influence the status code that we use in a response is the HTTP method that forms part of the request. Let’s have a look at those.

HTTP methods

An HTTP method indicates the sort of action that the request is for. The HTTP method in a request indicates whether we want to retrieve data e.g. a web page, whether we want to send new data e.g. a web form, or some other sort of action. To understand this “action” more fully, we can explore three frequently-used methods: GET, POST, and DELETE.

GET

GET is by far the most frequently used HTTP method. It is used to make a request that will GETthe resource located at a particular URL, and is used whenever we enter a URL in the browser and press enter. So when we type https://exts.epfl.ch into a browser, we are telling the browser to perform the request GET https://exts.epfl.ch. The response will be the matching resource: the EPFL Extension School homepage. We can see in the screenshot from Firefox Developer Tools earlier that the request is a GET request.

POST

POST is the method used in order to submit a form, or any time we want to send data from the browser to a web server. In some senses, POST is the compliment to GET: GET is used to retrieve existing data from a web server; POST is used to send new data to a web server.

The typical scenario that combines a GET request followed by a POST request is this: we visit a web page with a form on it using a GET request. Then we fill out the form and submit it. The browser will POST the data from the form to a particular URL. The web server will then process and hopefully save the data.

And a successful POST request will not normally respond with status code 200, but with 303 See other, which tells the browser to redirect to a different web page. So we can see how HTTP methods can influence the HTTP status code in the response.

DELETE

DELETE is obvious from the name: it is used to delete something on the server. HTML forms and controls aren’t actually capable of producing a request in DELETE format. In order to build a DELETE request, we have to use JavaScript. And here, we can appreciate the need to write code that will build the correct HTTP request — if we don’t write code that builds a request as a DELETE then the web server will not know that the request should delete a resource.

Other methods

Much like HTTP status codes, there are other HTTP methods that we use when building web applications, although the list is not as long. Each method has a particular use. When we build web apps with a web framework like Ruby on Rails, we make use of HTTP methods in order to control how people can interact with resources. HTTP methods and how they are used is therefore a crucial core concept that we need in order to build web applications correctly.

But equally, learning how to use a web framework like Rails can help us understand how and why we use a particular HTTP method in a particular scenario.

Be curious as you browse

That’s a brief overview of HTTP status codes and HTTP methods. We won’t get far as web developers without understanding how to use both of these as we build build web applications, so take the time to experiment and understand how them.

It’s easy to inspect HTTP status codes and methods using the Firefox Developer Tools so be curious as you browse the web. And developing with a web framework like Ruby on Rails helps build that understanding too, as you test out different pages and pieces of functionality.

Learn more!

Interested in learning more about Ruby, and Ruby on Rails? I teach Web Application Development at the EPFL Extension School, a certified online learning platform teaching digital skills in data science, web application development, and more. EPFL is one of the world’s leading universities and is ranked the “Number 1 Young University” by the Times Higher Education Ranking.

--

--