# Edit request body in Pre-request script

**URL:** <https://community.postman.com/t/edit-request-body-in-pre-request-script/17115>\
**Category:** Help Hub\
**Created:** [October 26, 2020, 11:11pm UTC](https://community.postman.com/t/edit-request-body-in-pre-request-script/17115 "2020-10-26T23:11:50Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![seitzcasey](https://avatars.discourse-cdn.com/v4/letter/s/f04885/32.png) [@seitzcasey](https://community.postman.com/u/seitzcasey)\
**Post date:** [October 26, 2020, 11:11pm UTC](https://community.postman.com/t/edit-request-body-in-pre-request-script/17115/1 "2020-10-26T23:11:50Z")

</div>

I’m trying to create a pre-request script for my collection as every request has some authentication data.

This is what I have:

```
let body = {
    mode: 'raw',
    raw: JSON.stringify({
        licenseUsername: 'username',
        licensePassword: 'password'
    }),
    options: {
        raw: {
            language: 'json'
        }
    }
}
pm.request.body.update(body);

```

---

<div class="post-metadata">

**Author:** ![meenakshi.dhanani](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/meenakshi.dhanani/32/5980_2.png) [@meenakshi.dhanani](https://community.postman.com/u/meenakshi.dhanani)\
**Post date:** [October 27, 2020, 11:32am UTC](https://community.postman.com/t/edit-request-body-in-pre-request-script/17115/2 "2020-10-27T11:32:57Z")

</div>

Hey @seitzcasey,  
You could also use variables to store the authentication data, so that in each request do you not have to repeat the username, password. You could read from the variable using  
`pm.variables.get("variable_key")`

You can find more here - [Variables in Postman](https://learning.postman.com/docs/sending-requests/variables/#defining-variables)

Let me know if that helps.

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [October 27, 2020, 11:39am UTC](https://community.postman.com/t/edit-request-body-in-pre-request-script/17115/3 "2020-10-27T11:39:45Z")

</div>

What I’ve found is the easiest is to just set it all to a variable. So take what you have and modify it slightly:

```auto
let body = {
    mode: 'raw',
    raw: JSON.stringify({
        licenseUsername: 'username',
        licensePassword: 'password'
    }),
    options: {
        raw: {
            language: 'json'
        }
    }
}
pm.variables.set('body', JSON.stringify(body));

```

Then in the body of your request, just use the variable  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/5/9/590ccf9108d738396e86abb2a6d3f676075eb925.png)

---

<div class="post-metadata">

**Author:** ![seitzcasey](https://avatars.discourse-cdn.com/v4/letter/s/f04885/32.png) [@seitzcasey](https://community.postman.com/u/seitzcasey)\
**Post date:** [October 27, 2020, 11:45am UTC](https://community.postman.com/t/edit-request-body-in-pre-request-script/17115/4 "2020-10-27T11:45:55Z")

</div>

I was hoping there was a way without having to do anything other that create the request inside my collection, set the endpoint and have it work. Is that possible?

---

<div class="post-metadata">

**Author:** ![odanylewycz](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/odanylewycz/32/3240_2.png) [@odanylewycz](https://community.postman.com/u/odanylewycz)\
**Post date:** [October 27, 2020, 11:56am UTC](https://community.postman.com/t/edit-request-body-in-pre-request-script/17115/5 "2020-10-27T11:56:23Z")

</div>

Hi @seitzcasey,

So, just to be clear, you want to define a “Pre-Request” script within the collection, and not create actual Postman Request objects within the collection (as in a Request in the UI?)

If thats the case, then the way you’d like to achieve that wouldn’t be possible. You need at least one request in the collection to run an actual request.

If this is not what you are asking, please clarify a bit more, and I’ll be glad to provide more help 🙂

---

<div class="post-metadata">

**Author:** ![seitzcasey](https://avatars.discourse-cdn.com/v4/letter/s/f04885/32.png) [@seitzcasey](https://community.postman.com/u/seitzcasey)\
**Post date:** [October 27, 2020, 12:09pm UTC](https://community.postman.com/t/edit-request-body-in-pre-request-script/17115/6 "2020-10-27T12:09:39Z")

</div>

No thats not what I meant. I want to avoid adding {{body}} each time I create a new request in postman. I want the collection level pre-request script to populate the request body without any extra action from the user.

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [October 27, 2020, 12:22pm UTC](https://community.postman.com/t/edit-request-body-in-pre-request-script/17115/7 "2020-10-27T12:22:05Z")

</div>

So is the body identical for every request in your collection then?

---

<div class="post-metadata">

**Author:** ![seitzcasey](https://avatars.discourse-cdn.com/v4/letter/s/f04885/32.png) [@seitzcasey](https://community.postman.com/u/seitzcasey)\
**Post date:** [October 27, 2020, 12:28pm UTC](https://community.postman.com/t/edit-request-body-in-pre-request-script/17115/8 "2020-10-27T12:28:15Z")

</div>

Yes that is correct.

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [October 27, 2020, 12:37pm UTC](https://community.postman.com/t/edit-request-body-in-pre-request-script/17115/9 "2020-10-27T12:37:49Z")

</div>

What was wrong with the approach you listed in your original post? What isn’t working with that?

---

<div class="post-metadata">

**Author:** ![seitzcasey](https://avatars.discourse-cdn.com/v4/letter/s/f04885/32.png) [@seitzcasey](https://community.postman.com/u/seitzcasey)\
**Post date:** [October 27, 2020, 12:49pm UTC](https://community.postman.com/t/edit-request-body-in-pre-request-script/17115/10 "2020-10-27T12:49:37Z")

</div>

The request doesn’t seem to be receiving the data. When I hit the postman echo endpoint, there are no args listed. But if I add them manually in the body tab, they do show.

---

<div class="post-metadata">

**Author:** ![seitzcasey](https://avatars.discourse-cdn.com/v4/letter/s/f04885/32.png) [@seitzcasey](https://community.postman.com/u/seitzcasey)\
**Post date:** [October 27, 2020, 1:32pm UTC](https://community.postman.com/t/edit-request-body-in-pre-request-script/17115/11 "2020-10-27T13:32:45Z")

</div>

Here is the manually entered body. As you can see, the “args” are correct.

 ![Untitled](https://us1.discourse-cdn.com/getpostman/original/3X/6/a/6a41cb6b85b4b3063218a3e744cdc57e38a1d2aa.png)

Here is the pre-request script for the collection that this request is in.

 ![Untitled](https://us1.discourse-cdn.com/getpostman/original/3X/7/9/79cd56b3c5e62e96ade6fae26a0148010ef670aa.png)

Here is the response. As you can see, the “args” are not correct even though the console shows the correct output.

 ![Untitled](https://us1.discourse-cdn.com/getpostman/original/3X/1/4/149be70f627eb9eeafdb9f7cc25cbd577f2c3812.png)

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [October 27, 2020, 1:52pm UTC](https://community.postman.com/t/edit-request-body-in-pre-request-script/17115/12 "2020-10-27T13:52:30Z")

</div>

You cannot change the request body in that way using `pm.request.body.update` - Although it might log that to the console, it won’t change the main body of the request that’s sent.

> <https://github.com/postmanlabs/postman-app-support/issues/4808>
>
> I want crypt the request body.
> For example,
> request body set to this:
> !\[image…\](https://user-images.githubusercontent.com/2630290/42554373-32d707e0-8517-11e8-80c0-64726b232b1b.png)
> 
> and pre-request script set to this:
> !\[image\](https://user-images.githubusercontent.com/2630290/42554480-a3d8c0a0-8517-11e8-9b39-b64d5989df6e.png)
> 
> and logged the "encryptData" value is encrypted. But the server received data is not encrypt, still is '{"a":"b"}'

---

<div class="post-metadata">

**Author:** ![seitzcasey](https://avatars.discourse-cdn.com/v4/letter/s/f04885/32.png) [@seitzcasey](https://community.postman.com/u/seitzcasey)\
**Post date:** [October 27, 2020, 1:59pm UTC](https://community.postman.com/t/edit-request-body-in-pre-request-script/17115/13 "2020-10-27T13:59:13Z")

</div>

Ah ok, thank you for the help.

---

<div class="post-metadata">

**Author:** ![yucapro](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/yucapro/32/7487_2.png) [@yucapro](https://community.postman.com/u/yucapro)\
**Post date:** [February 26, 2021, 8:08pm UTC](https://community.postman.com/t/edit-request-body-in-pre-request-script/17115/14 "2021-02-26T20:08:45Z")

</div>

I would like to add my support to this “feature request”. It would be great if we could change the request body before posting. For example :

`pm.request.body.raw = JSON.stringify(pm.request.body.raw);`

Is there any way to do this now?

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [February 27, 2021, 1:48pm UTC](https://community.postman.com/t/edit-request-body-in-pre-request-script/17115/15 "2021-02-27T13:48:03Z")

</div>

you can keep reqeust body as a vairable:

**say**

`{{requestbody}}`

now modify that variable instead and set the variable value as JSON.string using stringify:

` pm.environment.set("requestbody", JSON.stringify({"somekey": "somevalue"}))`

---

<div class="post-metadata">

**Author:** ![imcnicoll](https://avatars.discourse-cdn.com/v4/letter/i/7cd45c/32.png) [@imcnicoll](https://community.postman.com/u/imcnicoll)\
**Post date:** [February 28, 2021, 12:59pm UTC](https://community.postman.com/t/edit-request-body-in-pre-request-script/17115/16 "2021-02-28T12:59:32Z")

</div>

This works…

In Pre-request script, call a function defined in the Collection pre-request

```auto
//Formats the correct EHR_STATUS object template to adjust for differences between Better and ehrBase
pm.variables.set('ehrStatusTemplate',utils.formatEhrStatusTemplate(pm))

```

and the function in the Collection pre-request script is

```auto
utils = {
   formatEhrStatusTemplate: function(pm) {

        const useRandomSubjectId = false || pm.collectionVariables.get('UseRandomSubjectId') === 'true'

        const subbedSubjectId = pm.environment.replaceIn(useRandomSubjectId?'{{$guid}}':'{{subjectId}}')
       
        const ehrStatus = {
            _type: "EHR_STATUS",
            subject: {
                external_ref: {
                    id: {
                        _type: "HIER_OBJECT_ID",
                        value: subbedSubjectId,                 
                    },
                    namespace: '{{subjectNamespace}}',
                    type: "PERSON"
                }
            },
            is_modifiable: "true",
            is_queryable: "true"
        }                  
    
        console.log("esStatus",JSON.stringify(ehrStatus))

        let ehrStatusTemplate;
  
        if (!pm.environment.has('cdrProduct') || (pm.environment.get('cdrProduct') !== 'Better')) {
            const archetyped = {  
                    archetype_node_id: "openEHR-EHR-EHR_STATUS.generic.v1",
                    name: "ehr status",

            }
            ehrStatusTemplate = { ...ehrStatus , ...archetyped}
        }
        else {
           ehrStatusTemplate = ehrStatus
        }
        return JSON.stringify(ehrStatusTemplate)
    },

}

```

---

<div class="post-metadata">

**Author:** ![imcnicoll](https://avatars.discourse-cdn.com/v4/letter/i/7cd45c/32.png) [@imcnicoll](https://community.postman.com/u/imcnicoll)\
**Post date:** [February 28, 2021, 1:04pm UTC](https://community.postman.com/t/edit-request-body-in-pre-request-script/17115/17 "2021-02-28T13:04:00Z")

</div>

Hah - doing this helped me fix an annoying issue that you although pm is in scope for a variable.get(), unless you explicitly pass pm into the function, you cannot do a .set(), or at least it is not persisted.

Just goes to show if you give … you get back!!

---

<div class="post-metadata">

**Author:** ![jabarihunt](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jabarihunt/32/7875_2.png) [@jabarihunt](https://community.postman.com/u/jabarihunt)\
**Post date:** [March 30, 2021, 1:13am UTC](https://community.postman.com/t/edit-request-body-in-pre-request-script/17115/18 "2021-03-30T01:13:00Z")

</div>

You have to set variable placeholders in the body itself, then set the variable values in the pre-request script. So you request body will be:

```
{
    mode: 'raw',
    raw: JSON.stringify({
        licenseUsername: '{{username}}',
        licensePassword: '{{password}}'
    }),
    options: {
        raw: {
            language: 'json'
        }
    }
}

```

…and in you pre-request script:

```
pm.variables.set("licenseUsername", "username-value");
pm.variables.set("licensePassword", "password-value");

```

This allows the flexibility of changing the body structure down the line without having to mess with the values themselves and/or the script.

---

<div class="post-metadata">

**Author:** ![research-astronomer6](https://avatars.discourse-cdn.com/v4/letter/r/41988e/32.png) [@research-astronomer6](https://community.postman.com/u/research-astronomer6)\
**Post date:** [March 21, 2022, 2:02am UTC](https://community.postman.com/t/edit-request-body-in-pre-request-script/17115/19 "2022-03-21T02:02:42Z")

</div>

An example of “Set request body dynamically” is available in postman collection: [Postman](https://www.postman.com/postman/workspace/postman-answers/collection/9215231-33d1fd93-db48-4cba-8c2c-fcf96a013e70)

Created on 11 Mar 2021

---

<div class="post-metadata">

**Author:** ![dancervel](https://avatars.discourse-cdn.com/v4/letter/d/c57346/32.png) [@dancervel](https://community.postman.com/u/dancervel)\
**Post date:** [May 16, 2024, 8:57pm UTC](https://community.postman.com/t/edit-request-body-in-pre-request-script/17115/20 "2024-05-16T20:57:18Z")

</div>

Hi guys

Hi have a question, in the result from my system the file contains more segments, for example

{  
“load”: {  
“company\_code”: “247741”,  
“facility\_code”: “CICEMX2”,  
“load\_nbr”: “CSTR00000124”}  
}  
in this case only required 3 values: “company\_code”: ,  
“facility\_code”: y “load\_nbr”:

the second case  
{  
“oblpn\_list”: [  
{  
“oblpn\_nbr”: “OSTR00000001”,  
“oblpn\_inventory\_list”: [  
{  
“item\_barcode”: “910571230904102”,  
“curr\_qty”: “1”,  
“uom\_code”: “UNITS”  
},  
{  
“item\_barcode”: “910571230800378”,  
“curr\_qty”: “1”,  
“uom\_code”: “UNITS”  
},  
{  
“item\_barcode”: “910571230800293”,  
“curr\_qty”: “1”,  
“uom\_code”: “UNITS”  
},  
{  
“item\_barcode”: “910571230803903”,  
“curr\_qty”: “1”,  
“uom\_code”: “UNITS”  
},  
{  
“item\_barcode”: “910571230906121”,  
“curr\_qty”: “1”,  
“uom\_code”: “UNITS”  
}  
]  
}  
]  
}

the values requiered are:  
oblpn\_nbr, item\_barcode,curr\_qty y uom\_code

Do i need a script? because I have used var jsonData = pm.response.json(); only in Get mhetod

Could you help me please.

Regards

[Next page](https://community.postman.com/t/edit-request-body-in-pre-request-script/17115.md?page=2)
