0

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 Strings, 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

sjakovac
  • 124
  • 8
  • Doesn't adding `pm.variables.set('requestBody', JSON.stringify(pm.iterationData.get('var_name')));` to the `pre-request script` and then having `{{requestBody}}` in the Request Body work? You can't use `.set()` on `iterationData`, you need to use that on a different scope. – Danny Dainton Jul 16 '21 at 14:34
  • Yes, that does work, as I've written in question. I have it set up that way now, but I'm curious why modifying in `pm.iterationData` doesn't resolve well in *Body*, but it does resolve with `pm.iterationData.replaceIn("{{var_name}}")`. I agree that conceptualy `.set()` shouldn't be used on `iterationData`, but it runs successfully. I guess that's because it's of type `VariableScope` like other scopes – sjakovac Jul 16 '21 at 20:29
  • There's a difference between running successfully and it not doing anything when it runs. Set doesn't work in that scope, replaceIn works because it's just a different way of reading that same data. You didn't mention the way I have it in the original question. – Danny Dainton Jul 16 '21 at 20:52
  • So when Postman builds *Body* and resolves potential variables it doesn't use that `replaceIn` internally? Also, it's not true that `.set()` *didn't do anything when run* . It modified `value` in `values` of `pm.iterationData` (as shown in biggest codeblock in Q; it shows `pm.iterationData` before and after calling `.set()` on it). All in all, the thing in running well now, but I'm definitely intersted a bit more in internals – sjakovac Jul 16 '21 at 21:05
  • It might look like it changed something but I don't think that would have been sent as the request body. You question is difficult to read with the formatting you have applied to it so I'm not sure what's happening for you. At least you have it working now, maybe you can add that as the solution and accept your own answer. Might be useful to others. – Danny Dainton Jul 16 '21 at 21:32

0 Answers0