0
0
Postmantesting~5 mins

Send request block in Postman

Choose your learning style9 modes available
Introduction

We use the Send request block to make a call to a web service or API. It helps us check if the service works correctly by sending data and getting a response.

When you want to test if a website or app API returns the right data.
When you need to check if a server accepts your data and responds properly.
When you want to automate sending requests to an API during testing.
When you want to verify the status code and response time of an API.
When you want to test different inputs and see how the API reacts.
Syntax
Postman
pm.sendRequest({
    url: 'https://api.example.com/data',
    method: 'GET',
    header: {
        'Content-Type': 'application/json'
    },
    body: {
        mode: 'raw',
        raw: JSON.stringify({ key: 'value' })
    }
}, function (err, res) {
    // callback code here
});

The pm.sendRequest function sends the HTTP request.

The callback function receives error and response objects to check results.

Examples
Sends a GET request to get user data and prints the JSON response.
Postman
pm.sendRequest({
    url: 'https://api.example.com/users',
    method: 'GET'
}, function (err, res) {
    console.log(res.json());
});
Sends a POST request with JSON data to create a user and prints the status code.
Postman
pm.sendRequest({
    url: 'https://api.example.com/users',
    method: 'POST',
    header: {
        'Content-Type': 'application/json'
    },
    body: {
        mode: 'raw',
        raw: JSON.stringify({ name: 'Alice' })
    }
}, function (err, res) {
    console.log(res.status);
});
Sample Program

This script sends a GET request to a test API that echoes the request. It prints the status code and the response body.

Postman
pm.sendRequest({
    url: 'https://postman-echo.com/get?test=123',
    method: 'GET'
}, function (err, res) {
    if (err) {
        console.log('Request failed:', err);
    } else {
        console.log('Status code:', res.status);
        console.log('Response body:', res.text());
    }
});
OutputSuccess
Important Notes

Always check for errors in the callback to handle failed requests.

Use res.json() to parse JSON responses easily.

Set correct headers like Content-Type when sending data.

Summary

The Send request block lets you call APIs to test their behavior.

You write the request details and handle the response in a callback.

This helps you automate and verify API responses during testing.