Skip to content

Instantly share code, notes, and snippets.

@christian-korneck
Created June 11, 2022 03:10
Show Gist options
  • Save christian-korneck/41e454dd5ab397e02e75dd80f0b3ce3f to your computer and use it in GitHub Desktop.
Save christian-korneck/41e454dd5ab397e02e75dd80f0b3ce3f to your computer and use it in GitHub Desktop.
Postman - use output from request in another request (i.e. JWT token)

Postman - use request output as input for another request

Let's assume we request a JWT token from one endpoint and want to use it as Bearer Token in another request to a different endpoint.

We can:

  • 1. store the output (or part of it) in an environment variable

    • in the JWT request create a test and add some javascript. This selects the value of the JSON token {root}.token and stores it in a Postman env var token:
      token = pm.response.json().token;
      pm.environment.set("token", token);
      //console.log(pm.environment.get("token"));
      
      image
  • 2. we need to make sure an environment variable with this name exists in the currently selected Postman environment

    • in Postman in the top right corner click on the EYE button
    • if no environment is selected create a new one, else edit the existing one
    • in the environment settings create a variable named token and give it summy dummy initial value
    • click save
    • make sure the env is selected image
  • 3. we can use the variable anywhere in Postman with {{myvar}} - i.e. in the Authorization header

    • in our second request: Authorization -> Bearer and in the value field put {{token}} image

Now run the first request, then the second. It should work as expected.

For debugging use console.log() and view -> open postman console


Tags: Postman, Request Chaining, Request Piping, Piping, Chaining, Variable, Variables, Environment Variable, Pipe, API, Token, JWT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment