Open In App

Different kinds of HTTP requests

Improve
Improve
Like Article
Like
Save
Share
Report

HTTP (Hypertext Transfer Protocol) specifies a collection of request methods to specify what action is to be performed on a particular resource. The most commonly used HTTP request methods are GET, POST, PUT, PATCH, and DELETE. These are equivalent to the CRUD operations (create, read, update, and delete).

GET: GET request is used to read/retrieve data from a web server. GET returns an HTTP status code of 200 (OK) if the data is successfully retrieved from the server.

POST: POST request is used to send data (file, form data, etc.) to the server. On successful creation, it returns an HTTP status code of 201.

PUT: A PUT request is used to modify the data on the server. It replaces the entire content at a particular location with data that is passed in the body payload. If there are no resources that match the request, it will generate one.

PATCH: PATCH is similar to PUT request, but the only difference is, it modifies a part of the data. It will only replace the content that you want to update.

DELETE: A DELETE request is used to delete the data on the server at a specified location.

Now, let’s understand all of these request methods by example. We have set up a small application that includes a NodeJS server and MongoDB database. NodeJS server will handle all the requests and return back an appropriate response.

Setup and Installation:

Step 1: To start a NodeJS application, create a folder called RestAPI and run the following command.

npm init -y

Step 2: Using the following command, install the required npm packages.

npm install express body-parser mongoose

Step 3: In your project directory, create a file called index.js.

Project Structure: Our project directory should now look like this.

 

How to make a GET request

1. To make a GET request, paste the following URL in the Enter request URL text box of the postman. 

https://api.github.com/gists

2. Go to the headers tab and add a header called “Accept” and set it to:

application/vnd.github.v3+json

3. At the bottom, you can see the response formatted as JSON.

 

How to make a POST request

Steps to make a POST request are:

Step 1: Login to your GitHub account and go to Settings/Developer settings >> Personal access tokens.

Step 2: Click on Generate new token button.

 

Step 3: Give your token a name and select the scope Create gists.

 

 

Step 4: Click on Generate token button.

 

Step 5: Copy the access code and paste it somewhere.

 

Step 6: Finally, we are ready to make a POST request. Paste the same URL we used above in GET request in the input field of the postman.

Step 7: In the headers tab, add “Accept” as a header and set it to:

application/vnd.github.v3+json

Step 8: Go to the body tab, choose the content type to JSON and choose the data format as raw.

Step 9: Paste the following in the body tab.

{
    "public": true,
        "files": {
        "newgist.txt": {
            "content": "Adding a GIST via the API!!"
        }
    }
}

In the above text first, we have set the public flag to true which indicates that the gist we are creating is public. Second, we have defined the files property, whose value is another object with a key of the name of your new gist. Inside the new gist object, we have another key called content, whose value is whatever you want to write in your gist.

 

Step 10: Go to the authorization tab and choose the authorization type to Basic Auth. Type your GitHub username and generated access token in the username and password field, respectively.

 

Step 11: Click on the send button, and you will get the response.

 

How to make a PATCH request

Now we update the gist we just created. To make a PATCH request, follow the given steps:

Step 1: Find the ID of your created gist by going to

https://gist.github.com

 

 

Step 2: Paste the following URL in the input field of the Postman.

https://api.github.com/gists/{gist_id}

Step 3: In the headers tab, add “Accept” as a header and set it to 

application/vnd.github.v3+json

Step 4: Go to the body tab and add the description of the created gist.

{
    "description": this is my newly created gist,
        "files": {
        "newgist.txt": {
            "content": "Adding a GIST via the API!!"
        }
    }
}

Step 5: Go to the authorization tab and choose the authorization type to Basic Auth. Type your GitHub username and generated access token in the username and password field, respectively.

Step 6: Click on the send button and refresh your gist to see the updated description.

 

How to make a DELETE request

Step 1: Paste the following URL in the input field of the Postman.

https://api.github.com/gists/{gist_id}

Step 2: In the headers tab, add accept as a header and set it to

application/vnd.github.v3+json

Step 3: Click on the send button and your gist gets successfully deleted.



Last Updated : 31 May, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads