Can not deserialize instance of com.xxx.xxx.xxx.xxx.xx.xxx.SavedData out of START_ARRAY token
at [Source: io.undertow.servlet.spec.ServletInputStreamImpl@39123303; line: 2, column: 24] (through reference chain: com.xxx.xxx.xxx.xxx.xx.xxx.xxxx.SavedDataMap[“savedData”]->java.util.ArrayList[0])
I have a response of a API which looks like this on a GET - GET on API1
[
{
“id”: “emp1”,
“name”: suresh,
“address”: zxr street,
“zip”: 92111,
“city”: “fresno”,
“notes”: “employee of month”
If you don’t want to pollute your environment, please use collection variables when sharing data between requests in a collection! It’s the exact same thing but just pm.collectionVariables.set() instead of pm.environment.set()
let Cities = JSON.parse(responseBody),
CityIds = _.map(body, (city) => city.id);
// Store all the Cities Ids in the environment variable
pm.environment.set('CityIdsList', JSON.stringify(CityIds));
// Store the next index of the array for City ids
pm.environment.set('nextIndex', 0);
// Store the active City ID
pm.environment.set('activeCityId', CityIds[0]);
Hi @singhsivcan,
Is it possible to add Variable in the Pre-request script as well?
I want to add a variable for the URL parameter in the below pre-request script.
is it possible to store request json in some variable and set the variable in response Body for mock responses with example
i have req the that req or few of params i want to send in response(mocking response with example)
Yes it’s very much possible if you use environment variables.
While creating a mock you can choose the environment from the dropdown, and now keep storing your response JSON to this environment variable (remember to stringify it).
For eg: In your request you can do something like this:
let mockResponse = JSON.stringify(pm.response.json());
pm.environment.set('mockResponse', mockResponse);
And in your response body (make sure it’s type: raw) of the example you’ve to write something like this:
{{mockResponse}}
This variable will be resolved using the environment variable’s value every time you call it.
@cdxcz Ah, looking at the screenshot now I got it. I misunderstood your query, what now I understand is that you want to call the mock with some response, and the response of this mock should contain the authorization that you’ve set right?
Alright, so for that to work you can have 2 approaches but the common part remains the same: Common (Mandatory step):
Create a new mock with an environment associated (the mock will use this environment to resolve the variable from, otherwise it won’t work).
Approach 1: (Note: This will not work in the collection runner)
Create a new request let’s say ‘Req 1’ (with a dummy URL such as google.com) in which you set this environment variable.
Create a new request let’s say ‘Req 2’ which contains your mock URL, now call this mock URL and you’ll see the mock will resolve the variable value from the environment.
=> First hit “Send” on the “Req 1” and then hit “Send” for “Req 2”.
Approach 2: (More advanced and complicated, but will work in collection runner/monitor and just using a single request)
You’d have to use the Postman API. Grab an API key to use the Postman API and see the section in the documentation around updating an environment.
Use the pm.sendRequest function from your Pre-request Script to update the environment as soon as you run the request with the authorization header.
Using this approach, first, the environment will be updated by the Postman API in the cloud before the request is actually executed, and after that when the request is executed and hits the mock server of Postman in the cloud, the mock server will be able to resolve the updated environment value and thus return you the expected response.