Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL encode Postman variable?

Tags:

postman

I use Postman for REST API testing and parametrize tests with global variables.

I should put a phone number into GET request: /path/get?phone={{phone}} but leading + sign in the phone number is interpreted as a space.

What is the syntax to URL encode global variables in Postman? Is it possible to run JS encodeURIComponent() on variable in URL?

like image 483
gavenkoa Avatar asked Apr 25 '17 12:04

gavenkoa


People also ask

How do I send URL encoded in Postman?

URL-encoded data uses the same encoding as URL parameters. If your API requires url-encoded data, select x-www-form-urlencoded in the Body tab of your request. Enter your key-value pairs to send with the request and Postman will encode them before sending.

What is the difference between encodeURI and encodeURIComponent?

The difference between encodeURI and encodeURIComponent is encodeURIComponent encodes the entire string, where encodeURI ignores protocol prefix ('http://') and domain name. encodeURIComponent is designed to encode everything, where encodeURI ignores a URL's domain related roots.

How do I use encodeURIComponent in Javascript?

The encodeURIComponent() function encodes a URI by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two "surrogate" characters).


2 Answers

I am late but still worth it:

Just highlight and right click the part of url you want to encode. Select encodeURIComponent

That's it.

enter image description here

like image 56
Mohhamad Hasham Avatar answered Oct 01 '22 04:10

Mohhamad Hasham


Use the Pre-request scripts (it's next to body) for this:

var encoded = encodeURIComponent({{phone number}}); 

or

var encoded = encodeURIComponent(pm.environment.get("phone number")); 

and to proceed, use:

pm.environment.set("encoded phone number", encoded); 

And set your URL to /path/get?phone={{encoded phone number}}

like image 42
Shahar Hadas Avatar answered Oct 01 '22 02:10

Shahar Hadas