Search code examples
postman

Postman how to use parameters not set on the Query Params tab for a Post request


I've several request with 15-20 params on them, they get added to the request url at the end with a simple xxxx.json?param_name=param_value&next_param_name=next_param_value. A few of them i must update frequently, but many others are static and shared between several requests.

I would like to know if there is somwhere else where i can put said parameters so that the request can use them and I can enter the Query params tab and see only what i have to change. And if i can make it for a whole collection, better.


Solution

  • Query parameters in general have to be set through the Params tab, but you always have the option to dynamically construct/modify the request in a pre-request script. Prior to calling the pre-request script, the initial request is built up from the query and you could alter the request.

    Have your scripts add the query parameters by calling pm.request.AddQueryParams(). It can take a query string to be added, a key/value pair object for each parameter, or take an array of values. You can even mix and match if you wanted.

    pm.request.addQueryParams({
      key: 'staticParam1',
      value: 'staticParamValue1'
    });
    pm.request.addQueryParams(
      'staticParam2=staticParamValue2'
    )
    
    // adding multiple values
    pm.request.addQueryParams(
      'staticParam3=staticParamValue3&staticParam4=staticParamValue4'
    )
    pm.request.addQueryParams([
      'staticParam5=staticParamValue5',
      'staticParam6=staticParamValue6'
    ]);
    pm.request.addQueryParams([{
      key: 'staticParam7',
      value: 'staticParamValue7'
    }, {
      key: 'staticParam8',
      value: 'staticParamValue8'
    }]);
    
    // mix and matching
    pm.request.addQueryParams([{
      key: 'staticParam9',
      value: 'staticParamValue9'
    }, 'staticParam10=staticParamValue10']);
    

    You could store this pre-request script either in each query individually, or at a collection/folder level. Can even utilize environment/collection variables for these so you don't need to have these hard coded in your scripts.