Open In App

How to send different types of requests (GET, POST, PUT, DELETE) in Postman.

Last Updated : 01 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn how can we send different types of requests like GET, POST, PUT, and DELETE in the Postman. Postman is a popular API testing tool that is used to simplify the process of developing and testing APIs (Application Programming Interface). API acts as a bridge between two software applications which enables them to communicate and share data. Postman provides a simple Graphical User Interface for sending HTTP requests Like GET, POST, Put, Delete, and viewing their responses.

What are the Requests in the Postman?

In Postman, “requests” refer to the HTTP requests sent to a web server, which are used to perform various actions such as retrieving, creating, updating, or deleting resources. These requests correspond to different HTTP methods, each serving a specific purpose in the interaction with web services and APIs:

  • GET: Used to retrieve information from the given server using a given URI. GET requests should only retrieve data and have no other effect on the data.
  • POST: Sends data to a server to create or update a resource. The data is included in the body of the request. This may be a JSON payload, form data, or file.
  • PUT: Replaces all current representations of the target resource with the uploaded content. It’s used to update existing resources.
  • DELETE: Removes the specified resource from the server.

How To Send the Requests:

Before sending the requests first of all create a collection and add different requests to that collection. After that, we can use different requests.

1. How to send GET Request?

A GET request is used to retrieve data from a server.

  • Step 1: In Postman, from the collection add a new request and give a name get users. For this, we will be using the demo API to test the different types of requests.
  • Step 2: Select the GET method from the dropdown.
  • Step 3: Enter the URL of the API endpoint you wish to query.
  • Step 4: Click ‘Send’. The server’s response will be displayed in the lower pane.
Screenshot-2023-12-26-151039

GET request gives all the data from the server.

2. How to send POST Request?

POST requests are used to send data to a server.

  • Step 1: Create a new request as before.
  • Step 2: Select POST from the method dropdown.
  • Step 3: Enter the API endpoint URL.
  • Step 4: Under the ‘Body’ tab, choose a format (e.g., form-data, x-www-form-urlencoded, raw (JSON, text, JavaScript, etc.))
  • Step 5: Enter the data you wish to send and click ‘Send’.

Screenshot-2023-12-26-152702

3. How to send PUT Request ?

PUT requests are used to update existing data on the server. Follow the same steps as the POST request, but select PUT as the method.

Example: Ensure the URL and body data are appropriate for updating data.

Screenshot-2023-12-26-153431

4. How to send DELETE Request?

To delete data, use the same endpoints with an item ID.

Example: Send a DELETE request to https://jsonplaceholder.typicode.com/posts/1 to delete the post with ID 1.

Screenshot-2023-12-26-162657

Implementation using the ExpressJS:

In this example, we will implement a simple REST API and will implement the GET, POST, and DELETE request and we will also test it on the Postman, and observer the output. Here we will use the temperory memory to hold the items so we can work with that with different kind of API requests.

Steps to setup and test the different types of request with expressJS:

Step 1: Create a simple project, use the following command.

npm init -y

Step 2: There are two dependencies that we have to add in our project, first is express and another one is body-parser. Install these by running the following command.

npm install express body-parser

Step 3: Now create a simple javascript file in our case lets say app.js, and use the following code, in this we have implemented the bodyParser to read the json data and we have set up the GET, POST, and DELETE request.

Javascript




const express = require('express');
const app = express();
const bodyParser = require('body-parser');
 
app.use(bodyParser.json());
 
// In-memory data store
let posts = [
    {
        id: 1,
        title: 'Hello World'
    },
    {
        id: 2,
        title: 'Hello Node'
    },
    {
        id: 3,
        title: 'Hello Express'
    },
    // ... other posts
];
 
// GET request - Retrieve all posts
app.get('/posts', (req, res) => {
    res.json(posts);
});
 
// POST request - Create a new post
app.post('/posts', (req, res) => {
    const newPost = {
        id: posts.length + 1,
        title: req.body.title
    };
    posts.push(newPost);
    res.status(201).json(newPost);
});
 
// PUT request - Update an existing post
app.put('/posts/:id', (req, res) => {
    const post =
        posts.find(
            p =>
                p.id === parseInt(req.params.id)
        );
    if (!post) return res.status(404).send('Post not found.');
 
    post.title = req.body.title;
    res.json(post);
});
 
// DELETE request - Delete a post
app.delete('/posts/:id', (req, res) => {
    posts = posts
        .filter(p => p.id !== parseInt(req.params.id));
    res.status(204).send();
});
 
// Start the server
const port = process.env.PORT || 3000;
app.listen(port, () => {
    console.log(`Server is running on port ${port}`);
});


Step 4: Run the code using the following command.

node app.js

Output: After running the code, the server will run on the port : 300, and now we can use this link to test the API on the postman. http://localhost:3000/posts

GET Request : We can see in the image below that all the posts are displayed below.

Screenshot-2023-12-29-032649



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads