</>

Technology

Postman

Difficulty

Advanced

Interview Question

What is the purpose of the pm.sendRequest() in Postman and how do you use it?

Answer

pm.sendRequest() in Postman

pm.sendRequest() allows you to make asynchronous HTTP calls from within scripts (Pre-request or Tests). It is the key to advanced request chaining in Postman.

Primary Use Cases

  1. Fetch an auth token before the main request
  2. Set up test data (create a user before testing)
  3. Get dynamic values (timestamps, IDs) for use in the request
  4. Clean up data after a test

Syntax

JavaScript
pm.sendRequest(requestObject, callback);

// requestObject can be:
// 1. A URL string: "https://api.example.com/data"
// 2. A full request object with method, headers, body

Example 1: Fetch Auth Token Before Request

Put this in the Pre-request Script of your main request:

JavaScript
pm.sendRequest({
    url: pm.environment.get("base_url") + "/auth/login",
    method: "POST",
    header: {
        "Content-Type": "application/json"
    },
    body: {
        mode: "raw",
        raw: JSON.stringify({
            username: pm.environment.get("username"),
            password: pm.environment.get("password")
        })
    }
}, function (err, response) {
    if (err) {
        console.error("Login failed:", err);
        return;
    }

    const token = response.json().access_token;
    pm.environment.set("auth_token", token);
    console.log("Token fetched successfully");
});

// Now in the main request Headers:
// Authorization: Bearer {{auth_token}}

Example 2: Create Test Data Before Test

JavaScript
// Pre-request Script — create a user to test getting them
pm.sendRequest({
    url: pm.environment.get("base_url") + "/api/users",
    method: "POST",
    header: {
        "Content-Type": "application/json",
        "Authorization": "Bearer " + pm.environment.get("admin_token")
    },
    body: {
        mode: "raw",
        raw: JSON.stringify({
            name: "Test User " + Date.now(),
            email: "test_" + Date.now() + "@example.com"
        })
    }
}, function (err, res) {
    const newUserId = res.json().id;
    pm.environment.set("created_user_id", newUserId);
    console.log("Created test user with ID:", newUserId);
});

// Main request: GET {{base_url}}/api/users/{{created_user_id}}

Example 3: Clean Up After Test (in Tests tab)

JavaScript
// Tests tab — delete test data after validation
pm.test("User created successfully", function () {
    pm.response.to.have.status(201);
});

// Cleanup
const userId = pm.response.json().id;
pm.sendRequest({
    url: pm.environment.get("base_url") + "/api/users/" + userId,
    method: "DELETE",
    header: { "Authorization": "Bearer " + pm.environment.get("auth_token") }
}, function (err, res) {
    console.log("Cleanup: deleted user " + userId + ", status: " + res.code);
});

Key Points

FeatureDetail
LocationPre-request Script or Tests tab
AsyncYes — uses callback function
Error handlingCheck err parameter in callback
Use caseToken fetch, data setup, cleanup
AlternativeCollection Runner with ordered requests

Follow AutomateQA

Related Topics