Horje
How to Access Request Parameters in Postman?

Request parameters are additional pieces of information sent along with a URL to a server. They provide specific details about the request, influencing the server’s response. Parameters typically follow a ‘Key=Value’ format and are added to the URL in different ways depending on their type.

Query Parameters

These parameter are appended to the URL after a question mark (?). Used for optional information or filtering results.

Example: Fetching a list of users with a specific age range:

https://api.example.com/users?age_min=25&age_max=35

userId=12345 and status=active are query parameters.

Code Snippet: Extracting in Express

Node
const userId = req.query.userId;
const status = req.query.status;

Handling GET Requests with Query Parameters

Open Postman and create a new GET request:

  • Set the HTTP method to GET.
  • Enter the URL: https://postman-echo.com/get.
  • Click on the Params tab.
  • Add userId with value 12345 and status with value active.
  • Send the request.
Screenshot-2024-06-17-221859

Postman Screenshot for GET Request

Accessing Query Parameters in Postman

Direct Way : Automatically Detect when write full URL or Write it yourself in block

Screenshot-2024-06-17-221614

Using Pre-request Script : Add this in ”the’test’portion.

Node
let url = pm.request.url.toString();
let params = {};
if (url.includes('?')) {
    let queryString = url.split('?')[1];
    queryString.split('&').forEach(param => {
        let [key, value] = param.split('=');
        params[key] = value;
    });
}
console.log(params);
Screenshot-2024-06-17-222950

Example to showcase a server

create a server using express use of node js

App.js : having an endpoint ( GET )

Node
const express = require('express');
const app = express();

// Define a route to handle GET requests with query parameters
app.get('/api', (req, res) => {
  // Extract query parameters from the request
  const userId = req.query.userId;
  const status = req.query.status;

  // Log the extracted parameters to the console
  console.log(`UserId: ${userId}, Status: ${status}`);

  // Send a response back to the client with the received parameters
  res.send(`Received userId: ${userId}, status: ${status}`);
});

// Start the server on port 3000
app.listen(3000, () => {
  console.log('Server running on port 3000');
});

That Video shows, how postman extract automaticaly parameters from URL

Path Parameters

Embedded directly within the URL path, denoted by colons (:). Used to identify specific resources within a URL structure.

Example: Retrieving details of a user with a specific ID:

https://api.example.com/users/:id  (where ":id" is replaced with the actual user ID)

Code Snippet: Extracting in Express

Node
app.get('/api/users/:userId', (req, res) => {
    const userId = req.params.userId;
    res.send(`UserId: ${userId}`);
});

Handling GET Requests with Path Parameters

Open Postman and create a new GET request:

  • Set the HTTP method to GET.
  • Enter the URL: https://jsonplaceholder.typicode.com/todos/:id.
  • Click on the Params tab.
  • Add an ID with a value 5.
  • Send the request
Screenshot-2024-06-22-000254

Postman Screenshot for Path Parameters

Access Query Parameters in Postman

Direct Way : Gave input when writing full URL with ‘ :id ” , input id value

Screenshot-2024-06-21-235914

Using Pre-request Script : Add this in ”the’test’portion.

Screenshot-2024-06-22-000230

Body Parameters

Body parameters are included in the body of an HTTP request, typically used with POST, PUT, or PATCH requests. For example, sending JSON data in a POST request. typically in formats like JSON or form data. Used for sending the main data payload of the request.

Example: Creating a new user with name and email details:

https://api.example.com/login/

Body detail :

{
"name": "John Doe",
"email": "[email protected]"
}

Code Snippet:

Node
const { name, email } = req.body;

Handling POST Requests with Body Parameters

  • Set the HTTP method to POST.
  • Enter the URL: https://postman-echo.com/post.
  • Click on the Body tab.
  • Select raw and JSON formats.
  • Enter the JSON data:
{
"userId": "12345",
"status": "active"
}
  • Send the request
Screenshot-2024-06-21-234553

Postman Screenshot for Body Parameters:

Access Query Parameters in Postman

Direct Way : That’s what we already provided as input

Screenshot-2024-06-21-234506

Using Pre-request Script : Add this in ‘the test’ portion.

Node
const requestBody = pm.request.body.raw;
const bodyParams = JSON.parse(requestBody);
const userId = bodyParams.userId;
const status = bodyParams.status;

console.log(`UserId: ${userId}, Status: ${status}`);
Screenshot-2024-06-21-234419

Summary

Using pre-request scripts in Postman, you can access and manipulate request parameters before sending the actual request. This is useful for setting up test conditions, modifying requests dynamically, and performing pre-request validations. The examples provided show how to handle query, path, and body parameters, making your API testing more robust and flexible.




Reffered: https://www.geeksforgeeks.org


Web Technologies

Related
How to Create a Custom WordPress Login Page? How to Create a Custom WordPress Login Page?
E-commerce Websites : Shopping Cart E-commerce Websites : Shopping Cart
How to fix "Could not reliably determine the server’s fully qualified domain name" warning in Apache? How to fix "Could not reliably determine the server’s fully qualified domain name" warning in Apache?
E-commerce Websites : Product Detail Page E-commerce Websites : Product Detail Page
E-commerce Websites : Backend Setup E-commerce Websites : Backend Setup

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
17