How to get a request in Postman?

I am writing tests for Postman that generally work quite easily. However, now I want to access some query data, more precisely, the query parameter. 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 query parameters?

+11
source share
8 answers

If you want to extract the query string in URL-encoded format without parsing it. Here's how to do it:

pm.request.url.getQueryString() // example output: foo=1&bar=2&baz=3
+6
source

( POSTMAN). request.url 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

: Github Gist

+10

pm.request.url.query.all() . , :

var query = {};
pm.request.url.query.all().forEach((param) => { query[param.key] = param.value});
+10

, Postman - .

'Request' :

data {object} - . (request.data [ "key" ] == "value" ) headers {object} - (request.headers [ "key" ] == "value" ) {} - GET/POST/PUT ..
url {string} - URL- .

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

+4

pm.request.url.query PropertyList QueryParam. pm.request.url.query.get() pm.request.url.query.all(), . . PropertyList.

+3

:

console.log(request);

, . . request.name, . URL-, , (, javascript)

,

+1

, , URL-, / ,

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

URL- example.com , {}

URL- example.com?foo=bar

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

{ foo: 'bar' }

disabled :

enter image description here

+1

, :

- pm.request.url.query , , , . .. pm.request.url.query [0] ( .get(0)) , , , 0.

I have no idea why, but for some reason it is not at index 0, despite the statement of the debugger. Instead, you need to filter the request 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 a parameter with fields for key, value and disabled. getValueFromQuery returns only a value.

0
source

All Articles