secture & code

Resolving doubts about HTTP [Part 1].

Introduction

Why do I want to talk about HTTP? A few days ago I had a conversation with a colleague about what methods we should use in the REST API we are building. It was clear to both of us that POST is used to create a resource, DELETE to remove it and GET to obtain it. However, the discussion arose when we talked about how to update a resource. My colleague proposed using PATCH, while I commented that it depends on which part of the resource we want to update.

That conversation went on longer than I would have liked, so today I want to explain in a theoretical and practical way how to use these methods correctly, so that others don't have to go through such lengthy discussions.

What is HTTP?

As he explains well MDN, HTTP (Hypertext Transfer Protocol) is a network protocol for the transmission of hypermedia documents, such as HTML. Basically, it is the mechanism by which your browser communicates with the server of a web page when you visit it.

In the diagram I have generated, it shows how a client (browser) communicates with a server. It is a basic but detailed explanation that, although it is not the central purpose of this post, it is always useful to better understand the context.

http method

When creating a REST API, one of the key aspects is that HTTP provides us with a series of specific methods for each type of request. This can be seen in the diagram, where you see arrows representing requests such as GET, POST, PUT y DELETE.

If you open the browser's developer tools, in the Network tab you will see all the requests that the browser makes to the server. You will be able to see the URL and method of each request. For example, when I logged into Google, my browser's first request was a GET to a specific URL, and the server responded with a code 200 OK.

Screenshot 2024 11 11 at 08.34.55

Don't worry about the Status Codes in the image as that is a topic for another article in which I will explain what they are, which ones exist and when to use them.


¿What are HTTP methods and what do GET, POST, PUT, PATCH, DELETE mean??

An HTTP method is an indication of the action you want to perform on a resource in an API. These methods, also called HTTP verbs, include GET, POST, DELETE and others such as PUT, PATCH, HEAD, CONNECT, TRACE y OPTIONS, although not all of them are always used.

It is important to know that HTTP methods or verbs comply with some basic characteristics:

  • Insurancewhen they do not alter the state of the server.
  • Idempotentno matter how many times the resource is called with that method; it should always return the same result.
  • CacheablesThe response can be cached to improve performance.

What is each HTTP verb used for?

As mentioned above, each method has a specific theoretical and practical use:

  • GETThis method is used to get the information of a resource. Whenever a GET request is made for a resource, it should return the same thing as long as there have been no changes. For this reason, GET is a safe and idempotent verb.
  • POSTPOST: This verb is used to send data to the server, for example, to create a resource, such as a new user in our system. POST changes the state of the server and is therefore neither secure nor idempotent.
  • DELETEDELETE: Used to request the deletion of a resource. Although DELETE is not safe (because it changes the state of the system), it is idempotent, since requesting to delete a resource repeatedly (without errors) should give the same result.
  • PUTThis verb is idempotent and is used to create a resource if it does not exist or to completely replace an existing resource. Each time the PUT method is called, the resource is updated in the same way, maintaining idempotency.
  • PATCHPATCH: This verb allows partial modifications to a resource. For example, if we want to update only the email of a user, we can use PATCH to send only that portion of the resource we want to modify, in this case, the email.

As you can see, both PUT like PATCH can be used to update a resource, but each has its own specific purpose. For more information and detailed examples, you can consult the web site of MDN, where all verbs are explained in full.

Conclusion

Although the most common methods are POST y GET, There are other methods that provide clarity and semantics when developing an API. Each developer may have his or her own preference and some may use only PUT y GET, while others consider it sufficient to POST, PUT y GET. However, if you've made it this far, you'll know that there are more options and benefits to taking advantage of all the HTTP verbs.

Want to learn more about development and programming? If you didn't read our last article, you can find it here. here.

Full-Stack

Picture of David Pérez López

David Perez Lopez

I like projects to go well
Picture of David Pérez López

David Perez Lopez

I like projects to go well

We are HIRING!

What Can We Do