# Creating a url encoded request using post script

**URL:** <https://community.postman.com/t/creating-a-url-encoded-request-using-post-script/47149>\
**Category:** Help Hub\
**Created:** [May 5, 2023, 7:16pm UTC](https://community.postman.com/t/creating-a-url-encoded-request-using-post-script/47149 "2023-05-05T19:16:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![satellite-participa8](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@satellite-participa8](https://community.postman.com/u/satellite-participa8)\
**Post date:** [May 5, 2023, 7:16pm UTC](https://community.postman.com/t/creating-a-url-encoded-request-using-post-script/47149/1 "2023-05-05T19:16:03Z")

</div>

Hi Everyone,  
I just started using Postman some time ago. I have encountered an issue that I need some help with.

_My question_: I have defined an environment variable by the name of “form” whose value is  
{“MD”:“someValue”,“PaReq”:“someValue”,“TermUrl”:“someUrlHere”}

I’m trying to set the three values in a new request so that it looks something like this:

 ![Screenshot 2023-05-06 at 00.45.31](https://us1.discourse-cdn.com/getpostman/original/3X/a/5/a55d2ed9542fa9fbbb22cda325ca625d369ff5d5.png)

I need to do this using the pre-request script as the fields in “form” are not static. I also have to post these details on a URL that I got from a previous request and declared as a variable “url”.  
_I’ve already tried_:  
const forms = JSON.parse(pm.environment.get(“form”));  
pm.request.body.mode = “urlencoded”;  
pm.request.body.urlencoded = forms;

But unable to define any value in the request body. Please suggest.

 ![Screenshot 2023-05-06 at 00.44.16](https://us1.discourse-cdn.com/getpostman/original/3X/f/5/f539ca934e9fc36831e43c9c5f14cb2032f30248.png)

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [May 9, 2023, 12:11pm UTC](https://community.postman.com/t/creating-a-url-encoded-request-using-post-script/47149/2 "2023-05-09T12:11:57Z")

</div>

If you console log the body, you will see that urlencoded is an array of objects and it has a particular format.

```auto
console.log(pm.request.body.urlencoded);

```

![image](https://us1.discourse-cdn.com/getpostman/original/3X/6/7/67d60989a2ab5bf57d25ad4b86d87769d0fead95.png)

You can replace that entire array using the following.

```auto
const form = JSON.parse(pm.environment.get("form"));
let MD = form.MD;
let PaReq = form.PaReq;
let TermUrl = form.TermUrl;

let body = [
    {
        "key": "MD",
        "value": MD
    },
    {
        "key": "PaReq",
        "value": PaReq
    },
    {
        "key": "TermUrl",
        "value": TermUrl
    }
]

pm.request.body.urlencoded = body
console.log(pm.request.body.urlencoded) 

```

![image](https://us1.discourse-cdn.com/getpostman/original/3X/4/f/4f0bd73f19836e36bb4282951e4f522216f5c317.png)

---

<div class="post-metadata">

**Author:** ![satellite-participa8](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@satellite-participa8](https://community.postman.com/u/satellite-participa8)\
**Post date:** [May 9, 2023, 12:25pm UTC](https://community.postman.com/t/creating-a-url-encoded-request-using-post-script/47149/3 "2023-05-09T12:25:34Z")

</div>

@michaelderekjones Is there a way of handling this dynamically? As mentioned, the key-value pair in the form is not static.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [May 9, 2023, 12:30pm UTC](https://community.postman.com/t/creating-a-url-encoded-request-using-post-script/47149/4 "2023-05-09T12:30:06Z")

</div>

You will need to save the object in the environment variable in the correct format.

The same as the body I posted.

```auto
[{"key":"MD","value":"someValue"},{"key":"PaReq","value":"someValue"},{"key":"TermUrl","value":"someUrlHere"}]

```
