Asynchronous requests with Postman's PM API

0

Category : , , ,

You can send requests asynchronously with the pm API method sendRequest, these can be used in the pre-request or the test script.

Its important to note that if you send an asynch request in the pre-request tab "The main Postman request will NOT be sent until the pre-request script is determined to be finished with all callbacks, including sendRequest."

A blog post containing more detailed info on this can be seen on the below link:
http://blog.getpostman.com/2017/10/03/send-asynchronous-requests-with-postmans-pm-api/

I have only done basic testing with this but I could get the method to fire using the 2nd example of the 3 available on the previous url:
var headers = ['reseller_id:' + environment.booking_api_reseller_id];
    headers.push('request_id:'+ environment.booking_api_request_id);
    headers.push('request_authentication:'+ environment.booking_api_request_authentication);
console.log(headers);

// Example with a full fledged SDK Request
const echoPostRequest = {
  url: environment.booking_api_host + '/v1/Availability/product/' + environment.booking_api_availability_productKey + '?fromDateTime=' + environment.booking_api_availability_start_date + 
        '&toDateTime=' + environment.booking_api_availability_end_date,
  method: 'GET',
  header: headers,
  body: {
    mode: 'raw',
    raw: JSON.stringify({ key: 'this is json' })
  }
};

pm.sendRequest(echoPostRequest, function (err, res) {
    console.log('..............here........');
    console.log(err ? err : res.json());
});

I was having issues passing the headers to the request but i found the below url which states the header param should be an array.
http://www.postmanlabs.com/postman-collection/Request.html#~definition

Reuseable scripts in Postman

0

Category : , , ,

You can reuse methods across requests in postman.

Tip #5 in the below list:
http://blog.getpostman.com/2017/07/28/api-testing-tips-from-a-postman-professional/

  1. Init in Pre-Request or Tests tab or in a previous request.
  2. Store in an Environment or Global variable.
  3. Then call multiple times from other requests.
1) Setup method in Pre-Request or Tests tab in Postman, you can also list params to pass to method.
postman.setEnvironmentVariable("commonTests", (responseBody, environmentSchema) => {
    
    // parse response and log
    var responseObject = JSON.parse(responseBody);
    //console.log("response: " + JSON.stringify(responseObject));

    // test to check status code
    tests["Status code is 200"] = responseCode.code === 200;
    
    // test response time
    console.log("responseTime: " + responseTime);
    tests["Response time is less than " + environment.max_server_response_time + "ms"] = responseTime < environment.max_server_response_time;
    
    // validate schema
    eval(environment.validateSchema)(responseObject, environmentSchema);
});

2) Call method from the Pre-Request or Tests tab in Postman.
You can also call methods from within another method as you can see at the end of the previous code sample.

    // validate schema
    eval(environment.commonTests)(responseObject, environment.specificSchema);