Change request body in pre-request script

I am doing body level encryption in the pre-request script and want to the replace the entire reuqest body with encrypted data before sending the actual request

Hey @docking-module-geol4,
Welcome to the community :wave:
Is there anything, a question, or probably a hint as to where you’re trying to drive this discussion? Are you facing any issues with the pre-request script? We have some wonderful folks in the community who can help but make sure you mention what it is that you’re looking for.

1 Like

just provide a variable in place of request body:

{{requestBody}}

And modify this from pre-request script

pm.variable.set("requestBody", JSON.stringify( { "name":"test"} ))

1 Like

you can also modify it as :smiling_face_with_three_hearts:

pm.request.body='{"name":"age"}'

Let us if you would need any help :smiley:

2 Likes

I tried this and it didn’t work, instead this works

pm.request.body.raw = { name: “age” }

I want to append some items in the request body.

I tried this but not working

@mission-technologis9

How did you try this?

Can you provide a screenshot?

The above examples are replacing the body.

If you want to append, you can do it using the following method.

const body = JSON.parse(pm.request.body.raw); // retrieve current body
// update the body\object as appropriate
pm.request.body.raw = body; // write back updated body