How to assign a request payload value to a variable

Iโ€™m trying to get a value from a POST request payload to a variable to use in a testing script.is that possible? if is it possible, how can I do that?

Hi @yauwana.ravindra,

Is there a particular variable in the post request body that you want to save? If itโ€™s the whole post request body, you can assign that to a variable and reference that variable in the request body.

If you can explain more of the use case, I can try to help some more. But from my understanding, setting the whole request body as a variable and then referencing that in the body should suffice.

You can also creat it programmatically in the pre request script and assign it to a variable from there.

If so, I can give you some reference code.

Regards,
Orest

Hi @odanylewycz,

i figured it out by my self.I wanted to get a single value form the POST req. body and assign it to a variable in the Pre- req.

 var body = JSON.parse(request.data);
 console.log(body.listing.property.type);
 var type = body.listing.property.type;
 pm.globals.set("Prop_Type",type);

Thank you for your reply. I was searching for this for two days.Iโ€™m a newbie to POSTMAN

2 Likes

Hi @yauwana.ravindra,

I see now. Iโ€™m glad you were able to figure out the syntax you needed to get that value out from the request body and assign it to an environment variable.

Youโ€™re welcome! Nothing better than going through the process of figuring it out :smile:

If you need anymore help, Iโ€™ve made introduction and advanced videos on postman that might be helpful.

Additionally, @vdespa has a very good collection of postman videos on his channel.

Hope it helps!

Regards,
Orest

2 Likes