How I get request body params in Postman Tests tab?

I am writing tests collections for endpoints and I want the test to check if the response param estadoAula has the same value as the request param estadoAula so I can test everything went as intended. The param need to be sent in the body and not in the URL

Request Body
{
    "estadoAula": "1"
}
Response Body
{
    "idAula": "8d4cf346-cda0-47ca-acae-33981738b4b6",
    "estadoAula": "1"
}
Test

pm.test("Estado modificado correctamente",function(){
    var data = pm.response.json();
    let estadoAula = pm.request.body.estadoAula; <--- this doesn´t work, I need to get request param 'estadoAula' 
    pm.expect(data.estadoAula).to.eql(estadoAula);
});

Upvotes: 0

Views: 7170

Answers (1)

Sivcan Singh
Sivcan Singh

Reputation: 1892

You'll need to parse the request body, I am assuming you've set it to RAW along with 'JSON' as type.

This script should work for you:

pm.test("Estado modificado correctamente",function(){
    let data = pm.response.json(),
      requestBody = JSON.parse(pm.request.body.raw);

    pm.expect(data.estadoAula).to.eql(requestBody.estadoAula);
});

Upvotes: 6

Related Questions