Reputation: 58810
I use Postman to make a Post to one of my API to retrieve the Session ID
as you can see the
session is oVe6QiY89Coz1CmZffVwDHpUn76ql3rPy7xadAMynbX86FNJygw8UOcsup0B9yRzbrTALi9xkU9Kbb8ugEryfw==
I need to grab that, store it in a variable, and use it for my next POST request
How do I dynamically access that session value, base on my first response and continue to use it as for my next request ?
I added these 2 lines in my Tests tab
let response = pm.response.json().session;
console.log('session: ', response);
I got this to display in my Postman Console.
session:
oVe6QiY89Coz1CmZffVwDHpUn76ql3rPy7xadAMynbX86FNJygw8UOcsup0B9yRzbrTALi9xkU9Kbb8ugEryfw==
The question is, how do I use this session value and continue using it for my next POST request.
Do I need to create a Postman environment?
OR
Is this something that I can achieve by just writing a simple Pre-request script in Postman?
OR
Do I need to adjust any of my Postman settings?
Upvotes: 1
Views: 1314
Reputation: 25921
Following on from the previous answer - if you’re setting a variable in that way then you do need an environment file.
You can use globals
instead of environment
and you do not have to create the file. This variable will then be available to all requests in any part of Postman.
var jsonData = pm.response.json()
pm.globals.set('session', jsonData.session)
To access the value - All you need to do is use the {{session}}
syntax. This can be used in the URL, Headers, Request Body etc. As soon as you start typing the first set of curly braces, an auto complete box will appear with your values in it and also a few dynamic ones that are built in to the application.
This auto complete won’t happen in the request body but the variable syntax will still be accepted in the POST body.
Upvotes: 1
Reputation: 407
Looks like under Tests
you can parse the response save it in an environment variable. For step by step see this blog post
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("session", jsonData.session);
Upvotes: 1