32

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)?

lalithkumar
  • 3,480
  • 4
  • 24
  • 40
Deddiekoel
  • 1,939
  • 3
  • 17
  • 25

11 Answers11

37

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});
bbjay
  • 1,728
  • 3
  • 19
  • 35
  • Worked for me - and this solution is really elegant :) – fuzzi Apr 19 '18 at 15:11
  • Thanks. This is perfect. This should be the accepted answer. – Alex Roy Mar 20 '21 at 02:39
  • 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}), {})` – Bobby Nichols Mar 25 '21 at 23:25
  • The above one-liner is not (or no longer) required. `pm.request.url.query.toObject()` will return the same simple hash without the bloat. – Erhhung Jun 09 '21 at 23:08
23

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

Sudarsan GP
  • 1,194
  • 14
  • 22
17

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
Ikbel
  • 7,721
  • 3
  • 34
  • 45
  • 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 – Scaramouche Mar 26 '19 at 01:58
  • @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. – Ikbel Mar 26 '19 at 18:38
14

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.

HEX
  • 1,647
  • 2
  • 15
  • 29
10

Below one for postman 8.7 & up

var ref = pm.request.url.query.get('your-param-name');
Porali
  • 101
  • 1
  • 3
8

It's pretty simple - to access YOUR_PARAM value use pm.request.url.query.toObject().YOUR_PARAM

unickq
  • 1,497
  • 12
  • 18
5

I don't think there's any out of box property available in Postman request object for query parameter(s).

Currently four properties are associated with 'Request' object:

data {object} - this is a dictionary of form data for the request. (request.data[“key”]==”value”) headers {object} - this is a dictionary of headers for the request (request.headers[“key”]==”value”) method {string} - GET/POST/PUT etc.
url {string} - the url for the request.

Source: https://www.getpostman.com/docs/sandbox

Dinesh Kumar
  • 1,694
  • 15
  • 22
4

Bit late to the party here, but I've been using the following to get an array of url query params, looping over them and building a key/value pair with those that are

// the message is made up of the order/filter etc params
// params need to be put into alphabetical order
var current_message = '';
var query_params = postman.__execution.request.url.query;
var struct_params = {};

// make a simple struct of key/value pairs
query_params.each(function(param){
    // check if the key is not disabled
    if( !param.disabled ) {
        struct_params[ param.key ] = param.value;
    }
});

so if my url is example.com then the array is empty and the structure has nothing, {}

if the url is example.com?foo=bar then the array contains

{
  description: {},
  disabled:false
  key:"foo"
  value:"bar"
}

and my structure ends up being { foo: 'bar' }

Toggling the checkbox next to the property updates the disabled property:

enter image description here

ranieuwe
  • 2,268
  • 1
  • 24
  • 30
Pete
  • 4,542
  • 9
  • 43
  • 76
2

have a look in the console doing :

console.log(request);

it'll show you all you can get from request. Then you shall access the different parameters using request., ie. request.name if you want the test name. If you want a particular element in the url, I'm afraid you'll have to use some coding to obtain it (sorry I'm a beginner in javascript)

Hope this helps

Alexandre

A.Joly
  • 2,317
  • 2
  • 20
  • 25
  • 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. – Winter Dash Nov 19 '19 at 10:16
0

Older post, but I've gotten this to work:

For some reason the debugger sees pm.request.url.query as an array with the items you want, but as soon as you try to get an item from it, its always null. I.e. pm.request.url.query[0] (or .get(0)) will return null, despite the debugger showing it has something at 0.

I have no idea why, but for some reason, it is not at index 0, despite the debugger claiming it is. Instead, you need to filter the query first. Such as this:

var getParamFromQuery = function (key)
{
    var x = pm.request.url.query;

    var newArr = x.filter(function(item){
        return item != null && item.key == key;
    });

    return newArr[0];
};

var getValueFromQuery = function (key)
{
    return getParamFromQuery(key).value;  
};

var paxid = getValueFromQuery("paxid");

getParamFromQuery returns the parameter with the fields for key, value and disabled. getValueFromQuery returns just the value.

0

You could also get querystring and set it as a global variable like this:

postman.setGlobalVariable("SomeGlobalVariable",pm.request.url.query.get("queryParamName"));

Hamid Noahdi
  • 1,585
  • 2
  • 11
  • 18