Search code examples
postman

How to extract a specific value from a Request Body in POSTMAN and save in a variable


Is there any way to get a specific value (businessType) from the Request body and save in a variable in postman?

Request Body

{
    "fein": "nurrk",
    "businessType": "LLP",
    "incorporationYear": 1993,
    "incorporationState": "stateelesss",
    "annualRevenue": 1000,
    "companyActivity": "Producctiionn",
    "stockSymbol": "starer",
    "mcc": "qa12345"
}

POSTMAN SS

I've been using different commands and none of them seems to work

var reqBody = JSON.parse(request.data);

Solution

  • You can use pm.request to get data from the request body:

    let businessType = JSON.parse(pm.request.body.raw).businessType;
    
    pm.environment.set('businessType', businessType);
    

    More information about the pm.* API can be found here:

    https://learning.postman.com/docs/postman/scripts/postman-sandbox-api-reference/