How to run one request from another using Pre-request Script in Postman

后端 未结 6 1255

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.

6条回答
  •  时光取名叫无心
    2021-01-31 02:47

    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.

提交回复
热议问题