How to get to request parameters in Postman?

75,818

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

POSTMAN CLIENT CONSOLE RESPONSE

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

Share:
75,818

Related videos on Youtube

Deddiekoel
Author by

Deddiekoel

Updated on June 26, 2021

Comments

  • Deddiekoel
    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
    fuzzi about 6 years
    Worked for me - and this solution is really elegant :)
  • Scaramouche
    Scaramouche about 5 years
    hello, 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
    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
    Winter Dash over 4 years
    Exaclty 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
    Dan Gøran Lunde over 3 years
    Thanks, I needed a way to programmatically detect if a query parameter was disabled in Postman's GUI. This worked just fine.
  • Alex Roy
    Alex Roy about 3 years
    Thanks. This is perfect. This should be the accepted answer.
  • Bobby Nichols
    Bobby Nichols about 3 years
    If 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
    Erhhung almost 3 years
    The above one-liner is not (or no longer) required. pm.request.url.query.toObject() will return the same simple hash without the bloat.