Hello,
I want to save it as an environment variable so that there are no missing values from the values in the body of the response.
When specified as an environment variable, a portion of the body of the response is missing and saved as shown below.
===========================================
===========================================
scripts
var data = pm.response.json();
pm.environment.set(“Path”, data[“Path”]);
===========================================
environment variable
Path | 2024\11\19\sample.jpg
===========================================
Hi @altimetry-explorer-3.
Welcome to the Postman Community .
What does the full data you want to store look like? I will recommend storing JSON data as a string in environments oranywhere else in Postman.
\ is an escape character in JavaScript.
If you really need to store the double back slashes, then the following should work.
let path = "2024\\11\\19\\sample.jpg";
pm.collectionVariables.set("path", JSON.stringify(path).slice(1, -1));
console.log(pm.collectionVariables.get("path")); // 2024\\11\\19\\sample.jpg
Taken from…
Escaping Strings in JavaScript - Stack Overflow
However, it is worth noting that you are not storing the entire response, just one value.
To store the entire response as a string.
pm.collectionVariables.set("fullResponse",(JSON.stringify(pm.response.json())));
console.log(pm.collectionVariables.get("fullResponse")) // "{"Path":"2024\\11\\19\\sample.jpg"}"
The problem I can see with this is when you parse it, you are going to have the same problem and the leading escape character will be removed.
console.log(JSON.parse(pm.collectionVariables.get("fullResponse"))); // {Path: "2024\11\19\sample.jpg"}
Ideally if you have control of the API in question, you should speak to the developer and get them to encode the backslashes in the response using %5C
{
"Path": "2024%5C%5C11%5C%5C19%5C%5Csample.jpg"
}
You can then use the JavaScript decodeURI function.
console.log(decodeURI(pm.response.json().Path)); // "024\\11\\19\\sample.jpg"