Reputation: 1879
I'm using Postman and I am trying to chain together some requests. There is an identity string that is generated in my first request that I would like to use in my second request e.g. similar to searching for a product, and then adding that product to basket.
Now I've been able to pull the value from the first request and I can pick that up in the second request. The problem is that the identity string has an ampersand in it. When I post the second request, it throws an error because the ampersand has not been escaped in the string. I would like to replace the ampersand in the variable with "&" but I can't get this to work.
I'm new to JavaScript so I imagine this is where the problem is. In Postman I have:
var jsonObject = xml2Json(responseBody);
console.log(jsonObject);
postman.setEnvironmentVariable("ItineraryId", jsonObject.ItineraryId);
ItineraryId.replace("&","&");
This returns "There was an error in evaluating the test script: ItineraryId is not defined". So I tried:
var jsonObject = xml2Json(responseBody);
console.log(jsonObject);
var oldId = postman.setEnvironmentVariable("ItineraryId", jsonObject.ItineraryId);
oldId.replace("&","&");
And got "There was an error in evaluating the test script: Cannot read property 'replace' of undefined"
Thanks in advance.
Upvotes: 2
Views: 16817
Reputation: 1406
You should user pm.collectionVariables.set
like in example below :
pm.collectionVariables.set("userId", pm.response.headers.get('Location').replace(pm.environment.get("api_url") + "/api/users/", ""));
In this example after POST on /api/users
in response header : Location
we receive link to new resource. You can use other variables like : pm.environment.get("api_url")
Upvotes: 0
Reputation: 1879
Figured it out!
var jsonObject = xml2Json(responseBody);
console.log(jsonObject);
var itineraryId = jsonObject.ItineraryId;
itineraryId = itineraryId.replace("&","&");
postman.setEnvironmentVariable("ItineraryId", itineraryId);
Upvotes: 6