# Is there a way to automatically encode urls?

**URL:** <https://community.postman.com/t/is-there-a-way-to-automatically-encode-urls/874>\
**Category:** Help Hub\
**Tags:** request, url-encoding\
**Created:** [February 3, 2018, 6:23am UTC](https://community.postman.com/t/is-there-a-way-to-automatically-encode-urls/874 "2018-02-03T06:23:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcfarljw](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/mcfarljw/32/318_2.png) [@mcfarljw](https://community.postman.com/u/mcfarljw)\
**Post date:** [February 3, 2018, 6:23am UTC](https://community.postman.com/t/is-there-a-way-to-automatically-encode-urls/874/1 "2018-02-03T06:23:13Z")

</div>

I am working with some endpoints that take Chinese characters as part of the url parameter. Is there a way to automatically encode urls?

> /api/v1/word/你好

Normally I would just have the client encodeURI before sending the request. I’m finding it rather frustrating to have to manually convert all of my urls before using them with postman. It also turns things into something like `%E4%BD%A0%E5%A5%BD` which means I have to decode it again if I happen to forget what it was beforehand.

---

<div class="post-metadata">

**Author:** ![shamasis](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/shamasis/32/53_2.png) [@shamasis](https://community.postman.com/u/shamasis)\
**Post date:** [February 4, 2018, 9:21pm UTC](https://community.postman.com/t/is-there-a-way-to-automatically-encode-urls/874/2 "2018-02-04T21:21:15Z")

</div>

Is there a way where the Chinese part of the URL can be put inside a variable? If so, then you can use the pre-request script to read the variable and encode it. Albeit it becomes a bit unreadable compared to having the complete URL.

Also curious, these should get auto encoded, isn’t it @SamvelRaja?

---

<div class="post-metadata">

**Author:** ![rolfmadsen](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/rolfmadsen/32/952_2.png) [@rolfmadsen](https://community.postman.com/u/rolfmadsen)\
**Post date:** [September 26, 2018, 7:44am UTC](https://community.postman.com/t/is-there-a-way-to-automatically-encode-urls/874/3 "2018-09-26T07:44:55Z")

</div>

I had the same problem encoding search queries from a data file doing requests in the request runner.

\*\*\* Data file \*\*\*  
“ **q** ”,  
“你好”,

\*\*\* Pre request \*\*\*  
let encoded = encodeURIComponent(data. **q** );  
pm.environment.set(‘ **query** ’, encoded);

\*\*\* reqeuest URL \*\*\*  
/api/v1/word/{{ **query** }}

Hope this helps!

---

<div class="post-metadata">

**Author:** ![mramosr85](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/mramosr85/32/6912_2.png) [@mramosr85](https://community.postman.com/u/mramosr85)\
**Post date:** [January 12, 2021, 6:14pm UTC](https://community.postman.com/t/is-there-a-way-to-automatically-encode-urls/874/4 "2021-01-12T18:14:57Z")

</div>

`pm.request.url.query = encodeURIComponent(pm.request.url.query);`

This worked for me for URLs with query parameters which need encoding. As the query is a well-defined component of the URL, we can just encode that part using a pre-request script. No need to have variables there. This is not your exact case, but it might be useful for folks using URLS with query strings that need to be encoded.

---

<div class="post-metadata">

**Author:** ![mbuecher](https://avatars.discourse-cdn.com/v4/letter/m/9de0a6/32.png) [@mbuecher](https://community.postman.com/u/mbuecher)\
**Post date:** [November 25, 2021, 4:12pm UTC](https://community.postman.com/t/is-there-a-way-to-automatically-encode-urls/874/5 "2021-11-25T16:12:11Z")

</div>

That’s what actually works for me all the time with query parameters (not URL), but maybe adapted:

```auto
//console.log("----- Encode query string");
//console.log("Input: " + pm.request.url.query.toString());
var querycount = pm.request.url.query.count();
for(let i = 0; i < querycount; i++) {
  //console.log(i + "/" + pm.request.url.query.idx(i).key + ": " + encodeURIComponent(pm.request.url.query.idx(i).value));
  pm.request.url.query.idx(i).value = encodeURIComponent(pm.request.url.query.idx(i).value);
}
//console.log("Output: " + pm.request.url.query.toString());

```

---

<div class="post-metadata">

**Author:** ![moonsurfer](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/moonsurfer/32/11003_2.png) [@moonsurfer](https://community.postman.com/u/moonsurfer)\
**Post date:** [January 31, 2022, 2:14pm UTC](https://community.postman.com/t/is-there-a-way-to-automatically-encode-urls/874/6 "2022-01-31T14:14:52Z")

</div>

Thanks for sharing this!

---

<div class="post-metadata">

**Author:** ![djhouseknecht](https://avatars.discourse-cdn.com/v4/letter/d/90ced4/32.png) [@djhouseknecht](https://community.postman.com/u/djhouseknecht)\
**Post date:** [October 18, 2024, 4:15pm UTC](https://community.postman.com/t/is-there-a-way-to-automatically-encode-urls/874/7 "2024-10-18T16:15:09Z")

</div>

Absolute game changer 🍻

---

<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 18, 2024, 9:58pm UTC](https://community.postman.com/t/is-there-a-way-to-automatically-encode-urls/874/8 "2024-10-18T21:58:10Z")

</div>

Closed on October 18, 2024, 9:58pm UTC


