Postman Test getting request body named "email" problem

262 views Asked by At

i have these properties on my postman request body

{    
    "firstName": "QA",
    "lastName": "Test {{agent_phone_number}}",
    "email": "qatest-{{agent_phone_number}}@mail.com"
}

and i'm trying to get the request body on the Tests tab and set them in collection variable, the code loooks like this

const requestJson = JSON.parse(pm.request.body.raw);

pm.collectionVariables.set('agent_firstName', requestJson.firstName);
pm.collectionVariables.set('agent_lastName', requestJson.lastName);
pm.collectionVariables.set('agent_email'. requestJson.email);

when i run it, i got this error

TypeError: Cannot read property 'email' of undefined

When i checked the collection variable, the firstname and lastname are saved, but not the email..

is "email" something like "reserved" keyword on Postman? if so, then how do i get the "email" property from my request body?

1

There are 1 answers

0
anon On BEST ANSWER

You have a . instead of a , typo:

pm.collectionVariables.set('agent_email'. requestJson.email);

should be

pm.collectionVariables.set('agent_email', requestJson.email);