How to get to request parameters in Postman?
Solution 1
If you want to extract the query string in URL encoded format without parsing it. Here is how to do it:
pm.request.url.getQueryString() // example output: foo=1&bar=2&baz=3
Solution 2
The pm.request.url.query.all()
array holds all query params as objects.
To get the parameters as a dictionary you can use:
var query = {};
pm.request.url.query.all().forEach((param) => { query[param.key] = param.value});
Solution 3
I have been looking to access the request params for writing tests (in POSTMAN). I ended up parsing the request.url
which is available in POSTMAN.
const paramsString = request.url.split('?')[1];
const eachParamArray = paramsString.split('&');
let params = {};
eachParamArray.forEach((param) => {
const key = param.split('=')[0];
const value = param.split('=')[1];
Object.assign(params, {[key]: value});
});
console.log(params); // this is object with request params as key value pairs
edit: Added Github Gist
Solution 4
pm.request.url.query
returns PropertyList of QueryParam objects. You can get one parameter pm.request.url.query.get()
or all pm.request.url.query.all()
for example. See PropertyList
methods.
Solution 5
It's pretty simple - to access YOUR_PARAM value use
pm.request.url.query.toObject().YOUR_PARAM
Related videos on Youtube
Deddiekoel
Updated on June 26, 2021Comments
-
Deddiekoel almost 3 years
I'm writing tests for Postman which in general works pretty easily. However, I now want to access some of the data of the request, a query parameter to be exact. You can access the request URL through the "request.url" object which returns a String. Is there an easy way in Postman to parse this URL string to access the query parameter(s)?
-
fuzzi about 6 yearsWorked for me - and this solution is really elegant :)
-
Scaramouche about 5 yearshello, please what if I want to execute a script EVERY TIME the interceptor gets a post request and populate a js with the request params?? please, thank you
-
Ikbel about 5 years@Scaramouche I'm so sorry but I don't know how to do that. I think you should ask a separate question so that you can get some answers.
-
Winter Dash over 4 yearsExaclty what i've been looking for. Just add to complete: this "request" returned request as string. So at this time you cannot do something like request.myparam. But, you can parse this request, for instance:
let req = JSON.parse(request.data);
and then you can get any of request parametres by req.myParam. For instance testing between requests and response. -
Dan Gøran Lunde over 3 yearsThanks, I needed a way to programmatically detect if a query parameter was disabled in Postman's GUI. This worked just fine.
-
Alex Roy about 3 yearsThanks. This is perfect. This should be the accepted answer.
-
Bobby Nichols about 3 yearsIf you want to accomplish the same thing in a single line you can do
pm.request.url.query.all().reduce((paramObject, param) => ({...paramObject, [param.key]: param.value}), {})
-
Erhhung almost 3 yearsThe above one-liner is not (or no longer) required.
pm.request.url.query.toObject()
will return the same simple hash without the bloat.