2

App Details:

Postman for Win7 /x64

Issue Report:

  1. For Huge data request, I want to have my input in the form of external JSON file and indeed to re-place the entire body per iteration.
  2. Expected behavior: body of request, should able to replace by my variable when I run from Collections
  3. Screenshots : attached

Steps to reproduce

  1. My Json file in Collection runner as input data :

        [{
         "rename": {"AssetId": 5496,"NewName": "API-CK9-2st"},
         "addfolderto": {"FolderId": 5456,"FolderName": "API-CK1"}
        }]    
    
  2. And in My Pre-Request Script:

    var envRename = pm.iterationData.get("rename");
    pm.environment.set("rename", envRename );
    
  3. And in My Body, I'm trying to use directly the env. variable to replace body like :

     {{rename}}
    
  4. Then after running the script, my request body is replaced as:

        Request Body:"[object Object]"
    

    instead of my input json .

Screenshot link:

Postman Console log with my comments

Please help me to resolve this, for Huge data request, I want to have my input in the form of external JSON file and indeed to re-place the entire body per iteration.

Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
ChandraKanth
  • 66
  • 1
  • 6
  • This looks wrong to me in the pre-request script `{{envRename}}` the double curly brackets are not needed here. Also I don't believe that you use an object as the value in a data file - I might be wrong though. – Danny Dainton Apr 30 '18 at 12:42
  • Also you're picking from JSON, I think the way you're getting the date in the `rename` variable is not correct. – demouser123 Apr 30 '18 at 12:48
  • The variable from the file would be used anyway so you shouldn't need to get it and store it as a new environment variable in the pre-request script. – Danny Dainton Apr 30 '18 at 12:51
  • @DannyDainton: yes I have update the post, that was a typo `{{envRename}}` is not needed there. And its okay of using environment variable in the pre-request script just for modularity purpose and I dont think we can use directly `pm.iterationData.get("rename");` in body... POSTMAN is throwing error – ChandraKanth May 01 '18 at 04:14
  • I don't think you understand what I meant by that comment. Basically what you added in your pre-request script is pointless. If you deleted it and kept the `{{rename}}` in your request body, It would still pick up the data file variable when you execute it. – Danny Dainton May 01 '18 at 11:20

2 Answers2

1

You can use the JSON.stringify() function on the iterationData variable and then use the {{...}} syntax in the Request Body.

Here's an example of this locally using a different API but using the same approach it should work for what you need.

If you don't wrap your iterationData variable with the the JSON.stringify() function, you will see [object Object] sent as the POST Request Body data:

enter image description here


Here's my solution:

Adding the JSON.stringify() function to the Pre-Request Script tab and wrapping pm.interationData.get('my_object') with it like this:

enter image description here

Then add the {{new_object}} syntax variable to the Request Body, This value has now been JSON stringify'd:

enter image description here

In the Collection Runner, Select the Data File you wish to use. I'm using a JSON file with the key 'my_object' and the value as a JSON object rather than a single value - You can see the preview of this file here:

enter image description here

When the Collection or Request is run from the Collection Runner, it takes this object value and uses this in the Request Body. As I have used the JSON.stringify() function in the Pre-Request Script, it's not going to come through as the [object Object] that we saw at the start.

enter image description here

Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
  • :I have even used `JSON.stringify` for me no luck...and in my initial post there was a typo of using `{{envRename}}` instead of using var variable. So when you tried looks like you have also observed that its going as object right? if any solution please let me know. I hope you have seen the screenshot in the original post – ChandraKanth May 01 '18 at 04:22
  • This worked fine for me - I'll recreate my solution locally and update my answer. – Danny Dainton May 01 '18 at 09:44
  • Yeah sure, awaiting your replay – ChandraKanth May 05 '18 at 11:00
  • Updated my answer – Danny Dainton May 05 '18 at 13:02
  • No, that was still going as `[object, object]` – ChandraKanth May 11 '18 at 05:18
  • I don’t really see how that’s possible, did you follow the updated answer that I added - It clearly demonstrates that not happening. There must be something else that you’re doing it your set up that’s causing this not to happen. – Danny Dainton May 11 '18 at 06:26
1

It's necessary to change the variable name. So if iterationData contains rename variable then it should be renamed to foo. then in the body of the postman call, use {{foo}} The iterationData variable scope overrides all other scopes.

Jeff Groom
  • 11
  • 1