How to execute one request from another using Postman pre-script in Postman

I am trying to send an authenticated request with one click in the postman.

So, I have a query called "Oauth", and I use tests to store the token in a local variable.

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

Now I'm trying to automatically run an Oauth request (from a pre-request script) for any other requests that require a bearer token.

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

+26
source share
6 answers

NOTE. Now there is a way to do this in the pre-query script, see Other Answers . I will leave this answer for posterity, but everyone knows about it :)

I don’t think there is a way to do this in the preliminary request script, but you can get it in just a few clicks if you use the variable and the Tests tab. The Postman's blog has more complete instructions , but the gist of it is this:

  1. Configure the authentication request as usual.
  2. In the "Tests" section of this query, save the result of this query in a variable, perhaps something like the following:

     var data = JSON.parse(responseBody); postman.setEnvironmentVariable("token", data.token); 
  3. Run an authentication request - you should see that token installed for this environment (click the eye-shaped icon in the upper right corner).

  4. Configure the data request to use {{token}} where you previously inserted the bearer token.
  5. Run a data request - it should now be authenticated correctly.

To update the token, all you have to do is re-run the authentication request.

+16
source

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

+15
source

As mentioned by KBusc and inspired by these examples, you can accomplish your goal by installing a pre-query script, as shown below:

 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 simply refer to {{access_token}} like any other environment variable.

+11
source

You cannot send another request from the Pre-request Script section, but in fact it can be programmed and run one by one.

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

To view the results of the query, you can follow a different answer .

+4
source

You can add a preliminary request script to the collection, which will be executed before each request of the postman. 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) } }); } 
+1
source

I tried several solutions, the solution below is due to the fact that you are analyzing the response to request 1 and pass any variable to the second request parameter. (In this example, the variable is Last Name.)

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; 
0
source

All Articles