Rest API Methods
In this tutorial, we will learn about Rest API Methods.
HTTP Methods are the actions that can be performed on the
resource. We have below common HTTP
Methods that can be used
to perform actions on the resource:
Method | Description |
---|---|
GET | Used to retrieve/read data from a server |
POST | Used to create new resources on the server |
PUT | Used to update existing resources |
PATCH | Used to make partial update to a resource |
DELETE | Used to delete resources from the server |
HEAD | Similar to GET but returns only headers, no body |
OPTIONS | Used to describe method options for the resource |
GET Method
GET method is used to get the data from the server. Below are some examples of GET requests:GET /api/users/123
In the above example, we are passing 123
after /
to get the user with
id 123
. This is called path param or path variable. Path param is
used to pass the value in the URL path and used after
/
and users
.
GET /api/products?category=electronics
GET /api/products?category=electronics&brand=sony
In the above example, we are passing category=electronics
and
brand=sony
after ?
to
get
the products with category electronics
and with brand sony
.
This is called query param. Query
param is used
to pass the value in the URL query string and used after
?
and separated by &
.
GET /api/users
In the above example, we are not passing any path or query
param. This request will return all
the users from the server.
Note: We should not pass
sensitive information like passwords using GET method as
GET method will show the data in the URL hence the sensitive information can be seen by
others.
POST Method
POST method is used to create a new resource on the server.POST /api/users
{
"name": "John Doe",
"email": "john@example.com"
}
In the above example, we are passing the user details in the
request body to create a new
user
on the server. In POST method, we pass the data in the request body to the
server and then
server will
process the data and create a new resource.
PUT Method
PUT request to update user details:PUT /api/users/123
{
PUT /api/users/123
{
"name": "John Smith",
"email": "john.smith@example.com"
}
}
In the above example, we are passing the user details in the
request body to update the user
with id 123
.
Note: We pass
123
as a
path param and user
details
in the request body so that first the server will find
the user with id 123
and then update the user
details.
PATCH Method
PATCH method is used to make a partial update to a resource.PATCH /api/users/123
{
"email": "new.email@example.com"
}
In the above example, we are passing partial user details in the
request body to update the user
with id 123
.
Note: The difference
between PUT and PATCH is
that PUT is used to update the
entire resource whereas PATCH is used to update specific fields of the resource.
DELETE Method
DELETE method is used to delete resources from the server.:DELETE /api/users/123
In the above example, we are passing 123
as a path
param to delete the user with id 123
.
HEAD Method Example
HEAD method is used to fetch the headers of the resource.HEAD /api/users/123
The above request will return only the headers of the user with
id 123
. For Example:
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 123
OPTIONS Method Example
OPTIONS method is used to describe method options for the resource.OPTIONS /api/users
The above request will return the methods that can be used on
the resource. For Example:
HTTP/1.1 200 OK
Allow: GET, POST, PUT, DELETE, OPTIONS
Content-Length: 0
In the above response, the allow header will return the methods that can be used on the resource.
If you have liked our content, please share it with your friends
and colleagues.
Next we will learn about Rest API Status Codes.