38
loading...
This website collects cookies to deliver better user experience
HTTP defines a set of request methods to indicate the desired action to be performed for a given resource.
HTTP Methods | CRUD | What For? | Example Request URIs |
---|---|---|---|
GET |
Read/Retrieve | Retrieve the recipes from our server |
http://www.example.com/recipes or http://www.example.com/recipes/1
|
POST |
Create | Create a new recipe that is sent from client side |
http://www.example.com/recipes/new
|
PUT |
Update/Replace | Update an existing recipe that is sent from client side |
http://www.example.com/recipes/{:id}
|
PATCH |
Update/Modify | Update an existing recipe partially that is sent from client side |
http://www.example.com/recipes/{:id}
|
DELETE |
Delete | Remove/Delete an existing recipe from the resource |
http://www.example.com/recipes/{:id}
|
GET
requests to retrieve information only -- not to modify the information in any way. Since the GET
request does not modify anything, it is considered a "safe method". On top of that, GET APIs should be idempotent, which means making multiple identical requests must and will produce the same result. GET
method defined as "safe"?HEAD
, OPTIONS
and TRACE
in REST are also defined as "safe" methods. However, we will not cover these three methods in this article -- hopefully something I will cover in future articles! GET
request, if the resource is found on the server, it must return HTTP response code 200 (OK)
-- along with the response body, which is usually XML or JSON content. If the resource is not found, the server must return the infamous HTTP response code 404 (NOT FOUND)
. If the resource is determined that the GET
request is incorrectly formed, the server will return 409 (BAD REQUEST)
.axios({
method: 'get',
url: 'https://geek-jokes.sameerkumar.website/api?format=json',
});
POST
method because we want to create a new (subordinate) resource into the collection of resources, e.g. adding a newly found recipe to our collection of recipe! When creating new resource, the server will automatically assign an ID (new resource URI) to this new resource. 201 (CREATED)
, returning a location header with a link to the newly-created resource with the 201
HTTP code. (like https://www.example.com/recipes/1
)POST
methods are neither safe nor idempotent and invoking two identical POST
requests will result in two different resources containing the same information. axios('/login', {
firstName: 'Megan',
lastName: 'Lo'
});
PUT
method primarily to update existing resource. If the resource does not exist, then the API may decide to create a resource or not. On a successful update, the server will return 200 (OK)
, or 204 (NO CONTENT)
. If PUT
is used for creation and success, the server will return 201 (CREATED)
, like POST
.PUT
is not a safe operation, since it modifies (or creates) states within the resource, however it is idempotent. If you create or update a resource with the same cal again, the resource is still there and has the same state as it did in the same call. (It is however not idempotent, if you are trying to increment a state.)const article = { title: 'React PUT Request Example' };
axios.put('https://reqres.in/api/articles/1', article)
.then(response => this.setState({ updatedAt: response.data.updatedAt }));
GET
, POST
, PUT
are in this article. In the next article, I will like to dive into the difference between POST
and PUT
(common interview question). We will also discuss what PATCH
and DELETE
are.