HTTP Methods

The HTTP methods are often used to access a rest API and each one has its how behaviour and functionality. Let's get a look at the most used ones.

GET

Get is the most basic of all the methods and has the name that indicates it gets the info from the endpoint that usually is served as JSON on the body.

It's important to note that that the GET should only be used to retrieve data and never to change or update.

POST

The POST method is used to send data and create a new entry on the service.

The new info should be provided in the body of the request.

It's important to note that the POST is not idempotent, by this, we mean that if you send multiple requests with the same body it will create multiple entries.

For example, if we send 3 times the POST with the following body

{ "brand": "Opel", "color": "black", "cc": 1600 }

it will generate and store 3 different cars record on the DB with the same values.

PUT

Although very similar to the POST the PUT method has a different purpose, his purpose is to update an entry completely. Imagine in the previous example that we created a car on our DB but now we change the car and all the params need to be changed. For that goal, we gonna use the PUT with the following body

{ "brand": "Ferrari", "color": "red", "cc": 3000 }

Unlike the POST the PUT method is idempotent, doesn't matter how many times we do the request the output should update a single time.

PATCH

The PATCH method is also used for update an entry but in this case, only partially, continuing on the previous example of the car but on this case instead of updating the whole car we only wanna update the colour. For that, we use PATCH with the new colour on the body of the request. The API should know that the request expects to change the fields passed on the body and leave all the others untouched.

DELETE

This option just like the GET has its purpose written on the name itself. It deletes a specified resource, the ID of the resource to be deleted is passed on the request itself without a body.

DELETE /cars/:ID

Options

This method is not used as often as the previous ones but it's also extremely important.

The OPTIONS method serves as and handshake between the server and the client to share information about the communication that will be used by the other methods.

Some of that info is allowed headers and CORS info.

Wrap up

This was a quick overview of the most used methods to create a REST API, there are a couple more methods but they're rarely used and for the sake of simplicity we gonna keep them out.

In the future, we gonna discuss the best practices to create a REST API with examples including versioning and syntax of the requests.

Stay tuned.