admin管理员组

文章数量:1322342

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.

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.

Share Improve this question asked Jul 4, 2016 at 10:27 RookieRookie 1,8793 gold badges17 silver badges18 bronze badges 1
  • I think it maybe something to do with the type (if that's the correct terminology...). I added to my test: var oldStr = "identity&string"; var str = oldStr.replace("&","&"); tests["Is string escaped correctly?"] = str === "identity&string"; which works as expected (if I remove the amp; from the result, the test fails). – Rookie Commented Jul 4, 2016 at 11:44
Add a ment  | 

2 Answers 2

Reset to default 6

Figured it out!

var jsonObject = xml2Json(responseBody); console.log(jsonObject); var itineraryId = jsonObject.ItineraryId; itineraryId = itineraryId.replace("&","&"); postman.setEnvironmentVariable("ItineraryId", itineraryId);

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")

本文标签: javascriptIs it possible to modify a variable in PostmanStack Overflow