As a frontend developer you'll most likely be interacting with a lot of APIs. It's especially important to understand the different methods you can use when interacting with an API and the responses you receive back.
We'll be going over HTTP methods for REST APIs. So first let's talk about what in the world a REST API is.
REST stands for "representational state transfer" and is a set of rules developers need to follow when they create their API. REST APIs have five types of methods aka the type of request you send to the server.
Those methods are the following:
- GET
- POST
- PUT
- PATCH
- DELETE
Each method performs one of four possible actions:
- Create
- Read
- Update
- Delete
You may have heard these actions referred to as CRUD
before.
Let's dive into each method and what responses you get for both a successful and invalid request.
GET
What it does: Requests retrieve resource information.
Action: Read
Successful response: 200 OK
Error response: 404 not found
POST
What it does: The server creates a new entry in a database
Action: Create
Successful response: 201 Created
Error response: 404 not found or 409 conflict - if the resource already exists
PUT
What it does: Updates an existing resource
Action: Update
Successful response: 200 OK
Error response: 204 no content, 404 not found or 405 method not allowed
PATCH
What it does: Very similar to PUT
but makes a partial update on a resource
Action: Update
Successful response: 200 OK
Error response: 204 no content, 404 not found or 405 method not allowed
DELETE
What it does: Deletes resources
Action: Delete
Successful response: 200 OK
Error response: 404 not found or 405 method not allowed
A quick summary of the responses you may see is that anything in the 200 range means the request was successful, anything in the 400 range means an error has originated from the client and the 500 range means an error has originated from the server.
Have you stumbled upon any cool APIs that you've worked with before? I'd love to hear about them in the comments!
Be sure to follow me on Twitter for lots of posts about tech, and if I'm being honest, lots of posts about dogs too.
Top comments (7)
Hi, very good explanation.
Currenly I use APIs all the time, because we need connect the ERP with other services like invoices, reports, triggers. My question is what happend when you want to use GET but need to send a object (json) in the body. I think you can't send objects with GET then you need to use POST. is correct?
Can you use POST to
read
orremove
or is a bad practices?I used Signaturit to sign contracts or importants documents and their API is very easy to implement. Is very helpful in Europe because this company complies with European laws.
Regards.
Hi Jesús! If I'm understanding the question correctly, you'll need to do two separate requests. One with GET to retrieve the information you're requesting and then a POST to update that information.
Kudos for breaking this down in such a nice and legible way for beginners. I was happy with all of these and would have loved such a crisp and succinct break down when I was starting.
Thanks so much!
Nice answer Jesus!
We are using efirma under the same conditions
Great resource. I'll update my HTTP methods one-liner description in Postwoman with these.
hoppscotch / hoppscotch
👽 A free, fast and beautiful API request builder used by 75k+ developers. https://hoppscotch.io
A free, fast and beautiful API request builder
Helps you create requests faster, saving precious time on development - Subscribe
Built with ❤︎ by
liyasthomas and
contributors
Chat: Telegram, Discord
Donate: GitHub Sponsors, Open Collective, Patreon, PayPal
Table of contents
Features✨
Methods:
GET
- Retrieve information about the REST API resourceHEAD
- Retrieve response headers identical to those of a GET request, but without the response body.POST
- Create a REST API resourcePUT
- Update a REST API resourceDELETE
- Delete a REST…Thank you, that’s awesome!