# Change request body (RAW JSON) dynamicalls

**URL:** <https://community.postman.com/t/change-request-body-raw-json-dynamicalls/2043>\
**Category:** Help Hub\
**Tags:** pre-request, request\
**Created:** [July 6, 2018, 1:37pm UTC](https://community.postman.com/t/change-request-body-raw-json-dynamicalls/2043 "2018-07-06T13:37:02Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![prasu.eng123](https://avatars.discourse-cdn.com/v4/letter/p/e9bcb4/32.png) [@prasu.eng123](https://community.postman.com/u/prasu.eng123)\
**Post date:** [July 6, 2018, 1:37pm UTC](https://community.postman.com/t/change-request-body-raw-json-dynamicalls/2043/1 "2018-07-06T13:37:02Z")

</div>

Hi , the json body of my request keeps changing every now and then , I have over 20 to 30 collections which will use that particular request , is there a way i can change the request body from pre request script ? or any other way i can change the request body dynamically ? so that i dont want to go and change it every single time manually

---

<div class="post-metadata">

**Author:** ![kunagpal](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kunagpal/32/9_2.png) [@kunagpal](https://community.postman.com/u/kunagpal)\
**Post date:** [July 9, 2018, 7:09am UTC](https://community.postman.com/t/change-request-body-raw-json-dynamicalls/2043/2 "2018-07-09T07:09:59Z")

</div>

@prasu.eng123 The best way to do this is to break the body down into individual pieces and set their values using variables in pre-request scripts. Assuming that your request body takes the following form:

```json
{
  "foo": "bar"
}

```

If you’d like to make the value of the key `foo` dynamic, you’d update the request body to:

```json
{
  "foo": "{{foo}}"
}

```

Also, your pre-request code will have to include the following:

```auto
pm.environment.set('foo', 'THIS HAS TO BE REPLACED WITH YOUR VALUE');

```

---

<div class="post-metadata">

**Author:** ![prasu.eng123](https://avatars.discourse-cdn.com/v4/letter/p/e9bcb4/32.png) [@prasu.eng123](https://community.postman.com/u/prasu.eng123)\
**Post date:** [July 9, 2018, 2:00pm UTC](https://community.postman.com/t/change-request-body-raw-json-dynamicalls/2043/3 "2018-07-09T14:00:41Z")

</div>

@kunagpal : Thank for your reply , but this is not what i was looking for . I want to build the whole body dynamically, for example . the json body might sometimes take have 3 key value pairs or four key value pair etc

{  
id :“asdas”  
pwd:“asdas”  
auth: “ashd”  
}

or  
{  
id :“asd”  
pwd:“asdas”  
auth"adssa"  
newauth :“asdsa”  
}

is there any way i can write script to handle this ?

---

<div class="post-metadata">

**Author:** ![kunagpal](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kunagpal/32/9_2.png) [@kunagpal](https://community.postman.com/u/kunagpal)\
**Post date:** [July 9, 2018, 2:22pm UTC](https://community.postman.com/t/change-request-body-raw-json-dynamicalls/2043/4 "2018-07-09T14:22:54Z")

</div>

@prasu.eng123 This can be done as follows:

```auto
// in the pre-request script
var body = {}; // this will contain all the necessary keys

pm.environment.set('req_body', JSON.stringify(body));

```

In the request body (notice how the `req_body` variable is referenced directy in the raw body):

```json
{{req_body}}

```

---

<div class="post-metadata">

**Author:** ![prasu.eng123](https://avatars.discourse-cdn.com/v4/letter/p/e9bcb4/32.png) [@prasu.eng123](https://community.postman.com/u/prasu.eng123)\
**Post date:** [July 9, 2018, 2:55pm UTC](https://community.postman.com/t/change-request-body-raw-json-dynamicalls/2043/5 "2018-07-09T14:55:11Z")

</div>

@kunagpal i am not able to understand what you have posted , could you please elaborate a bit ? may be with an example

---

<div class="post-metadata">

**Author:** ![kunagpal](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kunagpal/32/9_2.png) [@kunagpal](https://community.postman.com/u/kunagpal)\
**Post date:** [July 9, 2018, 3:51pm UTC](https://community.postman.com/t/change-request-body-raw-json-dynamicalls/2043/6 "2018-07-09T15:51:55Z")

</div>

@prasu.eng123 Sure thing. As we need to dynamically set the entire request body, we’ll have to save it to a variable that can be used inside the request body editor. The flow goes something like this:

1. Build the body in the form of a JSON object. This happens inside the pre-request script.

```auto
var body = {
  id: 'asdas',
  pwd: 'asdas',
  auth: 'ashd'
};

```

1. Stringify the body, using [JSON.stringify](https://developer.mozilla.org/enUS/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify). This converts objects to strings.

```auto
var body_str = JSON.stringify(body); // body was defined in the previous step.

```

1. Save the stringified body as an environment variable using `pm.environment.set`;

```auto
pm.environment.set('request_body', body_str); // this sets an environment variable with the stringified body

```

1. In the request body editor, specify the `raw` request body as the variable created in step 3.

```auto
{{request_body}}

```

Note that the snippet from step 4 will have to be placed in the body editor.

EDIT: Check this collection to see it working directly in Postman, you can also fork it to adapt it to your own use-case:

[https://www.postman.com/postman/workspace/postman-answers/collection/9215231-b9133e48-73c3-4aa4-b189-e038ee4c5e00?ctx=documentation](https://www.postman.com/postman/workspace/postman-answers/collection/9215231-b9133e48-73c3-4aa4-b189-e038ee4c5e00?ctx=documentation)

---

<div class="post-metadata">

**Author:** ![prasu.eng123](https://avatars.discourse-cdn.com/v4/letter/p/e9bcb4/32.png) [@prasu.eng123](https://community.postman.com/u/prasu.eng123)\
**Post date:** [July 9, 2018, 4:31pm UTC](https://community.postman.com/t/change-request-body-raw-json-dynamicalls/2043/7 "2018-07-09T16:31:26Z")

</div>

Thank you @kunagpal . But i am getting the new line paramaters like \n and\r how can i eleiminate them , any idea ?

---

<div class="post-metadata">

**Author:** ![kunagpal](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kunagpal/32/9_2.png) [@kunagpal](https://community.postman.com/u/kunagpal)\
**Post date:** [July 9, 2018, 4:41pm UTC](https://community.postman.com/t/change-request-body-raw-json-dynamicalls/2043/8 "2018-07-09T16:41:45Z")

</div>

@prasu.eng123 Where exactly are you getting these values?

---

<div class="post-metadata">

**Author:** ![prasu.eng123](https://avatars.discourse-cdn.com/v4/letter/p/e9bcb4/32.png) [@prasu.eng123](https://community.postman.com/u/prasu.eng123)\
**Post date:** [July 9, 2018, 4:55pm UTC](https://community.postman.com/t/change-request-body-raw-json-dynamicalls/2043/9 "2018-07-09T16:55:49Z")

</div>

@kunagpal i have a sample request from postman , whose response is the same as data u send. Example beloww . as u can see the data field in response contains the data with \n ad \r

**Request** : [postman-echo.com/post](http://postman-echo.com/post)

Pre request:

```auto
var body = {foo: "1", password: "asas"}; 

pm.environment.set('req_body', JSON.stringify(body));
pm.variables.get("variable_key");

```

Response:

```auto
{
    "args": {},
    "data": "\r\n{\"foo\":\"1\",\"password\":\"asas\"}",
    "files": {},
    "form": {},
    "headers": {
        "host": "postman-echo.com",
        "content-length": "31",
        "accept": "*/*",
        "accept-encoding": "gzip, deflate",
        "authorization": "Basic Og==",
        "cache-control": "no-cache",
        "content-type": "text/plain",
        "cookie": "sails.sid=s%3AlnH3kM7X-xeDKcn6G0hehamaL_Mu-ELn.sKV0Fi5CRHiTsT0fUiY7xgA%2BBVjIR5PNXa3JHjYC7ok",
        "postman-token": "077d84f1-e8b9-431b-8870-96f080914896",
        "user-agent": "PostmanRuntime/7.1.5",
        "x-forwarded-port": "80",
        "x-forwarded-proto": "http"
    },
    "json": null,

}

```

---

<div class="post-metadata">

**Author:** ![kunagpal](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kunagpal/32/9_2.png) [@kunagpal](https://community.postman.com/u/kunagpal)\
**Post date:** [July 10, 2018, 3:16am UTC](https://community.postman.com/t/change-request-body-raw-json-dynamicalls/2043/10 "2018-07-10T03:16:20Z")

</div>

@prasu.eng123 This is happening because the request body is being sent as text instead of JSON. You can fix this by clicking on the mode dropdown to the right of the request body and selecting `application/json`, as can be seen below:

 ![27%20AM](https://us1.discourse-cdn.com/getpostman/original/3X/b/e/be3da539dfdd45e7b3520d76049ff85d70bf7416.png)

---

<div class="post-metadata">

**Author:** ![Lokesh0999](https://avatars.discourse-cdn.com/v4/letter/l/fbc32d/32.png) [@Lokesh0999](https://community.postman.com/u/Lokesh0999)\
**Post date:** [July 12, 2019, 12:08pm UTC](https://community.postman.com/t/change-request-body-raw-json-dynamicalls/2043/11 "2019-07-12T12:08:20Z")

</div>

Hello @kunagpal,  
Thanks for the snippet,  
Could you please explain how can we set dynamic AlphaNumeric string value which is incremental(Letters+Number) to a Json object of the request. Ex: “OrderId”: “ThisValue01234”.

---

<div class="post-metadata">

**Author:** ![obisignano-c](https://avatars.discourse-cdn.com/v4/letter/o/a8b319/32.png) [@obisignano-c](https://community.postman.com/u/obisignano-c)\
**Post date:** [September 17, 2019, 10:21pm UTC](https://community.postman.com/t/change-request-body-raw-json-dynamicalls/2043/12 "2019-09-17T22:21:36Z")

</div>

Hi. Why wouldn’t this work on the pre-request script? It’s in the SDK documentation.

console.log(pm.request);  
pm.request.body.raw = ‘Your Request’;  
console.log(pm.request);

---

<div class="post-metadata">

**Author:** ![zmes50416](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/zmes50416/32/3870_2.png) [@zmes50416](https://community.postman.com/u/zmes50416)\
**Post date:** [February 24, 2020, 8:53am UTC](https://community.postman.com/t/change-request-body-raw-json-dynamicalls/2043/13 "2020-02-24T08:53:13Z")

</div>

I found this post when I trying to construct my request from the pre-request script.

Even though using environment variables can work around the issue, but I think @obisignano-c 's suggestion will make Postman more useful to the developers.

Nevertheless, thank you guys for this great software 🙂

---

<div class="post-metadata">

**Author:** ![frankdoylezw](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/frankdoylezw/32/7322_2.png) [@frankdoylezw](https://community.postman.com/u/frankdoylezw)\
**Post date:** [April 15, 2020, 10:01am UTC](https://community.postman.com/t/change-request-body-raw-json-dynamicalls/2043/14 "2020-04-15T10:01:45Z")

</div>

@kunagpal What happens when the value that you want to replace is not a string?

---

<div class="post-metadata">

**Author:** ![kevin.swiber](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kevin.swiber/32/3820_2.png) [@kevin.swiber](https://community.postman.com/u/kevin.swiber)\
**Post date:** [April 15, 2020, 11:09pm UTC](https://community.postman.com/t/change-request-body-raw-json-dynamicalls/2043/15 "2020-04-15T23:09:08Z")

</div>

@frankdoylezw,

Just don’t include quotes. Example:

```json
{
  "foo": {{foo}}
}

```

---

<div class="post-metadata">

**Author:** ![soomro.ikram](https://avatars.discourse-cdn.com/v4/letter/s/c0e974/32.png) [@soomro.ikram](https://community.postman.com/u/soomro.ikram)\
**Post date:** [May 21, 2020, 11:48pm UTC](https://community.postman.com/t/change-request-body-raw-json-dynamicalls/2043/16 "2020-05-21T23:48:28Z")

</div>

perfect, worked for me.  
Set::TESTS  
pm.environment.set(‘req\_body’, pm.request.body.raw);  
Use:: RAW\>JSON  
{{req\_body}}

---

<div class="post-metadata">

**Author:** ![nwamtf1](https://avatars.discourse-cdn.com/v4/letter/n/74df32/32.png) [@nwamtf1](https://community.postman.com/u/nwamtf1)\
**Post date:** [December 13, 2021, 7:42am UTC](https://community.postman.com/t/change-request-body-raw-json-dynamicalls/2043/17 "2021-12-13T07:42:43Z")

</div>

Dear all guys:

Here is my script which will set the request RAW JSON body.

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/6/625fa32c50d08abcfca4888b5fba252949a8b1d8.png)

pm.request.body.mode = “raw”;  
pm.request.headers.upsert({key: “Content-Type”, value: “application/json”});  
var raw\_body = {“account”: “username1”};  
var body\_json = JSON.stringify(raw\_body);  
pm.request.body.raw = body\_json;

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [August 23, 2023, 3:44pm UTC](https://community.postman.com/t/change-request-body-raw-json-dynamicalls/2043/18 "2023-08-23T15:44:24Z")

</div>

Closed on August 23, 2023, 3:44pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
