50
votes

I'm trying to send an authenticated request with one click in postman.

So, I have request named "Oauth" and I'm using Tests to store the token in a local variable.

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("token", jsonData.access_token);

What I'm trying to do now is that run the Oauth request automatically (from a pre-request script) for any other requests which needs a bearer token.

Is there a way to get an access token and send an authenticated request with one postman button click?

7

7 Answers

39
votes

As mentioned by KBusc and inspired from those examples you can achieve your goal by setting a pre-request script like the following:

pm.sendRequest({
    url: pm.environment.get("token_url"),
    method: 'GET',
    header: {
        'Authorization': 'Basic xxxxxxxxxx==',
    }
}, function (err, res) {
    pm.environment.set("access_token", res.json().token);
});

Then you just reference {{access_token}} as any other environment variable.

25
votes

A little late but for others who come across this post, it IS now possible to send another request from the Pre-request Script section. A few examples can be found here : https://gist.github.com/madebysid/b57985b0649d3407a7aa9de1bd327990

25
votes

NOTE: There now is a way to do this in a pre-request script, see the other answers. I'll keep this answer for posterity but just so everyone knows :)

I don't think there's a way to do this in the pre-request script just yet, but you can get it down to just a few clicks if you use a variable and the Tests tab. There are fuller instructions on the Postman blog, but the gist of it is:

  1. Set up your authentication request like normal.
  2. In the Tests section of that request, store the result of that request in a variable, possibly something like the following:

    var data = JSON.parse(responseBody);
    postman.setEnvironmentVariable("token", data.token);
    
  3. Run the authentication request -- you should now see that token is set for that environment (click on the eye-shaped icon in the top right).

  4. Set up your data request to use {{token}} wherever you had previously been pasting in the bearer token.
  5. Run your data request -- it should now be properly authenticated.

To refresh the token, all you should need to do is re-run the authentication request.

7
votes

You can't send another request from Pre-request Script section, but in fact, it's possible to chain request and run one after another.

You collect your request into collection and run it with Collection Runner.

To view request results you can follow other answer.

4
votes

You can add a pre-request script to the collection which will execute prior to each Postman request. For example, I use the following to return an access token from Apigee

const echoPostRequest = {
  url: client_credentials_url,
  method: 'POST',
  header: 
      'Authorization: Basic *Basic Authentication string*'

};

var getToken = true;

if (!pm.environment.get('token'))

{
    console.log('Token  missing')

}
else 
{

    console.log('Token all good');
}

if (getToken === true) {
    pm.sendRequest(echoPostRequest, function (err, res) {
    console.log(err ? err : res.json());
        if (err === null) {
            console.log('Saving the token');
            console.log(res);
            var responseJson = res.json();
            console.log(responseJson.access_token);
            pm.environment.set('token', responseJson.access_token)


        }
    });
}
0
votes

You can use a function sendRequest of postman.

Some examples here.

https://gist.github.com/madebysid/b57985b0649d3407a7aa9de1bd327990

-1
votes

I have tried multiple solutions, the below solution is related to when you are parsing the response for request 1 and passing any variable into the second request parameter. ( In this Example variable is Lastname. )

Note:- data and user are JSON objects.``

postman.clearGlobalVariable("variable_key");
postman.clearEnvironmentVariable("variable_key");
tests["Body matches string"] = responseBody.has("enter the match string ");
 var jsonData = JSON.parse(responseBody);
  var result = jsonData.data;
  var lastName = result.user.lastName;
tests["Body matches lastName "] = responseBody.has(lastName);
tests["print  matches lastName " + lastName ] = lastName;