Is it possible to manipulate the JSON via script before sending the request

Hi all,

I have a postman request and was able to use a CSV input data to supply variable values to run. This is great where we just want to run with different values for some key in the JSON. However, for my test cases there are scenarios where some child element should NOT be there. In this case it’s not just a matter of substituting a variable with a value but rather the entire “key” : “value” entry should be omitted.

How do I achieve this? Can this be done via the Pre-request Script or some other tricks?

Thanks everyone in advance.

Yes in pre request u can modify request body as pm.request.body= :partying_face:

1 Like

Thank you! I will explore this.

1 Like

If I use newman would I be able to read a JSON (which is to be manipulated) from the local drive as well as the data (CSV) file?

OK, so I manipulated and updated the request body as below:

let requestData = JSON.parse(pm.request.body);

some manipulation codes here …

pm.request.body = JSON.stringify(requestData);

It seems that doing so automatically changed the content-type in the header to “text/plain”.

How do I change the content type in the header back to “application/json”?

You need to parse and update the raw element of the body, which will leave everything else including the header intact.

const body = JSON.parse(pm.request.body.raw);
// some manipulation here.
pm.request.body.raw = body;