Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postman sendRequest use authorization from collection

I can send a request in a Postman pre-request script as seen below. What I would like to do is use the same authentication as is set in the Collection so that if the Collection changes then my sendRequest follows suite.

pm.sendRequest({
    url: 'http://some_url',
    method: 'GET',
    header: {
        'content-type': 'application/json',
        'authorization': 'Basic ' + auth
    },

}, function (err, res) {
    // do something
});

The reason I want to do this is that I want to share the collection with partners and customers and each of them may use a different authentication type. At the moment I have configured my collection to use basic authentication and have used variables for user name and password. If, for example, a partner switches the collection to use OAuth then they will need to also update all of my pre-request scripts to use OAuth.

If pm.sendRequest() was able to inherit the authentication just as each request in the collection can then the partner could make the change in one place.

like image 872
David Churchland Avatar asked Feb 22 '19 03:02

David Churchland


1 Answers

This will work, assuming you're executing sendRequest after a request which had an authorization header as well:

pm.sendRequest({
    url: 'http://some_url',
    method: 'GET',
    header: {
        'content-type': 'application/json',
        'authorization': request.headers["authorization"]
    },

}, function (err, res) {
    // do something
});

Information on the Request object can be found here.

like image 109
Josh Avatar answered Oct 11 '22 05:10

Josh