I'm using Postman's Collection Runner on Destop client with json
test data file:
[{"var_name": {"key_1": 1, "key_2": 2}}]
I'd like to inject that JSON
into the body for POST
request. In Body section of request I've put {{var_name}}
and set option raw
and type as JSON
.
When I send request and inspect request body it is the following: [object Object]
. I recognize it's probably result of calling toString()
on object that doesn't provide it. Quick check in pre-request script:
var var_name = pm.iterationData.get("var_name")
console.log(typeof var_name, var_name)
Yields:
object {key_1: 1, key_2: 2}
So, it seems like during the loading of test data, value under key var_name
has been parsed into an object. As I understood it, Postman variables should only hold String
s, so I expected test data loading to do conversion to comply on it's own. Tried to modify it manualy doing the following in pre-request script:
var var_name = pm.iterationData.get("var_name")
pm.iterationData.set("var_name", JSON.stringify(var_name))
var_name = pm.iterationData.get("var_name")
console.log(typeof var_name, var_name)
Writes on console: string {"key_1":1,"key_2":2}
but request body is still [object Object]
. It seems as tough nothing has changed. But checking console.log(pm.iterationData)
before and after modification shows contrary:
{
id: "6127a367-b4ad-4d46-9081-c0a833cb5231",
values: [
{
type: "any",
value: {key_1: 1, key_2: 2},
key: "var_name"
}
]
}
{
id: "6127a367-b4ad-4d46-9081-c0a833cb5231"
values: [
{
type: "any",
value: "{"key_1":1,"key_2":2}",
key: "var_name"
}
]
}
To make matters even more confusing, when console.log(pm.iterationData.replaceIn("{{var_name}}"))
, after modifying var_name
data-scope variable, gives "{"key_1":1,"key_2":2}"
which is what I wanted. It seems like the change isn't visible for {{var_name}}
in Body. I've tried disabling/enabling varibale tracing for pm.iterationData
but with same results.
I could circumvent whole problem by defining values directly in test data JSON
file as [{"var_name": "{\"key_1\": 1, \"key_2\": 2}}]
or by adding body manualy, but that's not what I'm looking for. Problem also goes away if I store modified value in pm.variable
so it shadows the one in Data scope, but I don't like that solution either.
I've tried adding toString
function to prototype of var var_name
but get same result, works on console and not in Body