How to get to request parameters in Postman?

Viewed 82034

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

10 Answers

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

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

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

Below one for postman 8.7 & up

var ref = pm.request.url.query.get('your-param-name');

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

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.

Related