URL encode Postman variable?
Asked Answered
H

13

100

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?

Hilaire answered 25/4, 2017 at 12:53 Comment(1)
I opened an issue to postman to add a flag in the path parameter setupHallee
R
101

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}}

Retort answered 25/4, 2017 at 12:55 Comment(4)
Will not this affect the remaining request, will it get corruptedQuarrel
I don’t understand the questionRetort
This isn't resulting the results I am expecting. is there a way to see the value of "encoded phone number"? ThanksLafreniere
@Lafreniere Open Postman Console (menu -> View -> Show Postman Console / Ctrl+Alt+C), you'll see the request paths with the "encoded phone number".Fractostratus
P
128

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

Prostomium answered 12/10, 2019 at 9:4 Comment(6)
this is the best way.Lelandleler
easily the best wayAquaplane
The question was about variables, that is, stuff like {{defined_someplace_else}}. You can't encode them like that because the value inside that variable needs to be encoded, not the variable reference.Kiley
This is the best way for static parameters. If you try it on a variable in that spot, it encodes the braces that identify it as a variable, not the contents of the variable. Doesn't answer the question (but still helpful for lots of people that find this answer).Speckle
this is the wayCourageous
..did you even read the question?Bashan
R
101

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}}

Retort answered 25/4, 2017 at 12:55 Comment(4)
Will not this affect the remaining request, will it get corruptedQuarrel
I don’t understand the questionRetort
This isn't resulting the results I am expecting. is there a way to see the value of "encoded phone number"? ThanksLafreniere
@Lafreniere Open Postman Console (menu -> View -> Show Postman Console / Ctrl+Alt+C), you'll see the request paths with the "encoded phone number".Fractostratus
R
17

Just a shortcut to Mohhamad Hasham' answer.

You can encode and decode direct in the Params Value field: enter image description here

Raspberry answered 4/11, 2020 at 11:1 Comment(2)
It doesn't answer the question of encoding content from variable reference {{ var }}. There is no problem to encode static piece of text.Hilaire
@Hilaire you are right - I was misguided by the popular answer that helped me.Raspberry
V
10

The trick is to get your environment variable in the pre-request script and then set it after encoding it

    var encoded = encodeURIComponent(pm.environment.get("phone"));
    pm.environment.set("encoded phone number", encoded);
Venita answered 1/12, 2017 at 12:33 Comment(0)
C
6

If you are automating Bearer and need encoding you can do this in pre-request script

var encodedUser = encodeURIComponent(pm.request.url.query.get("userName"));
var encodedPass = encodeURIComponent(pm.request.url.query.get("userPassword"));

pm.request.url.query.remove("userName");
pm.request.url.query.remove("userPassword");

pm.request.url.query.insert({key:"userName",value: encodedUser});
pm.request.url.query.insert({key:"userPassword",value:encodedPass});
Corelation answered 15/6, 2023 at 11:47 Comment(0)
D
3

The problem with right-click => Encode URI Component is that it destroys the raw value of that parameter. You can use the following pre-request script to overcome this (which also works for cases where you have disabled that param):

// queryParam is of type https://www.postmanlabs.com/postman-collection/QueryParam.html
if ((queryParam = pm.request.url.query.one("name_of_your_query_param")) !== undefined
    && queryParam.disabled !== true) {
    queryParam.value = encodeURIComponent(queryParam.value);
}

Demers answered 23/1, 2023 at 18:37 Comment(0)
I
2

This will work as well:

var encoded = encodeURIComponent(pm.request.url.query.get("phone"));
pm.request.url.query.remove("phone");
pm.request.url.query.insert("phone", encoded);
Isolde answered 2/2, 2022 at 20:59 Comment(0)
T
2

For the postman version 9.28.4 ==>

You can use 2 methods:

  1. By selecting the part of the url in url bar -> right click -> EncodeURLComponent. (screenshot attached)

Demonstration method#1

  1. You can also use "pre-request script" tab of postman and write the script for the variable manually. (screenshot attached)

Demonstration method#2

Tranquilizer answered 23/11, 2022 at 6:13 Comment(0)
K
2

Switch the toggle on for encoding path and all params in request. Switch the toggle on

Kristelkristen answered 23/2, 2023 at 12:2 Comment(1)
In version 10.21.9, this option isn't sufficient: some characters are "forgotten", like the ":" which is not automatically encoded by Postman (tested on a timestamp, format UTC -> "2024-01-10T14:30:45.282Z").Portugal
E
1

I came across this question looking for an answer to a similar question. For me, the variable was a JSON object. The endpoint I needed to hit was expecting an object list as a query parameter and I have no way to change that to be the request body.

As much as some of the answers helped, I ended up coming up with a combined solution. Also, some of the code given in other answers is outdated as Postman has updated their API over the years, so this uses methods that work on 7.22.1.

pm.environment.set("basicJSON", '[{"key1":"value1","key2":"value2"},{"key1":"value1","key2":"value2"}]')
var encoded = encodedURIComponent(pm.environment.get("basicJSON"))
pm.environment.set("encodedJSON", encoded)

This solution requires that both basicJSON and encodedJSON exist as environment variables. But what was important for me was the ease of editing the object. I didn't want to have to decode/encode constantly to change values, and I didn't want to have to open the environment variables dialogue. Also, it's important to note the single-quotes around the object. Excluding them or using double-quotes would cause Postman to send something like "[object Object]" which is useless to an endpoint expecting actual JSON.

Exanthema answered 21/4, 2020 at 15:13 Comment(0)
A
1

I had similar problem with braces { and } in query parameter.
By turning off the following setting it started working for me.

enter image description here

Astri answered 18/2, 2021 at 3:44 Comment(0)
A
0

Universal Pre-request Script:

var encoded = encodeURIComponent(pm.request.url.query);
pm.request.url.query.clear();
pm.request.url.query.populate(encoded);
Ahrens answered 29/4 at 17:1 Comment(0)
S
-2

Click the Params button to open the data editor for URL parameters. When you add key-value pairs, Postman combines everything in the query string above. If your URL already has parameters - for example, if you are pasting a URL from some other source. Postman splits the URL into pairs automatically. https://www.getpostman.com/docs/v6/postman/sending_api_requests/requests

Sprinkling answered 4/5, 2018 at 9:37 Comment(1)
This does not answer the question. These docs describe how to add urlencoded form parameters, whereas the question is about urlencoding GET parameters set from environment variables.Anserine

© 2022 - 2024 — McMap. All rights reserved.