I have a Postman collection that consists of 15 post requests. One of my requests is a POST request that generate an access token which expires in 3 minutes and it's response looks like this:
"access_token": "accessTokenValue",
"expires_in": 300
After running this I need all the other requests to extract the access token from this response body and use it to send those requests.
I have used following code to retrieve the access_token
from the first request:
pm.test("response is ok", ()=>{
if( pm.response.to.have.status(200)){
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("access_token", jsonData.access_token);}})
Now, what I want to do is add the access token I retrieved from here and add to the other request. I have gone through following documents,
https://blog.postman.com/extracting-data-from-responses-and-chaining-requests/
https://community.postman.com/t/how-to-extract-post-request-field-values-in-other-get-tests/17391
and few more and failed to understand how this works.
What did I miss in this process?
You've already extracted the token here.
postman.setEnvironmentVariable("access_token", jsonData.access_token);}})
What you haven't mentioned is how the other requests need to use the token.
I suspect it needs to be set as a Bearer Token in the header for the subsequent requests which can be done using the Authorization helpers.
This can be added on the individual requests, or at the folder\collection level if it affects more than one request.
You can also use the Authorization helper to create the token.