Sobes.tech
Middle

How to pass parameters from the response of one request to another request in API auto-testing in Postman?

sobes.tech AI

Answer from AI

  1. Getting the value from the first response:

    In the "Tests" section of the first request:

    // Parse JSON response
    const responseJson = pm.response.json();
    
    // Get the value of the required field (e.g., id)
    const itemId = responseJson.id;
    
    // Set an environment variable or a global variable
    pm.environment.set("itemId", itemId);
    // Or for a global variable: pm.globals.set("itemId", itemId);
    
  2. Using the parameter in the second request:

    In the second request, in the URL, body, or headers, use the syntax {{variableName}}:

    For example, in the URL:

    GET /api/items/{{itemId}}

    When running the collection or sequence of requests, Postman will automatically substitute the value of the "itemId" variable set in the first request.

Additionally: You can use the "Pre-request Script" tab in the second request for more complex logic before it executes, if necessary. For example:

// Get the value of the environment variable
const itemId = pm.environment.get("itemId");

// Or get the value of the global variable: const itemId = pm.globals.get("itemId");

// Perform some actions with the obtained value before sending the request
// For example, log it
console.log("Using item ID:", itemId);