Postman - How to replace values for all specific properties in the JSON Response, to use it later as another Request's Body

依然范特西╮ 提交于 2019-12-11 18:07:10

问题


I have 2 API requests. 1st one is GET, that returns a response. This response is used as a Body/Payload in the 2nd request (POST). BUT the Payload should have certain values to be replaced before used in the 2nd request (in my case below it should be value for "Status" property).

How can I do it?

Here is my example Response:

{  
   "Variations":[  
      {  
         "ItemIds":[  
            "xxx"
         ],
         "Items":[  
            {  
               "Id":"67-V1",
               "GuId":"xxx",
               "Type":"Unit",
               "Status":"Active"
            }
         ],
         "Name":"VAR 1",
         "Id":"67-V1"
      },
      {  
         "ItemIds":[  
            "yyy"
         ],
         "Items":[  
            {  
               "Id":"67-V2",
               "GuId":"yyy",
               "Type":"Unit",
               "Status":"Active"
            }
         ],
         "Name":"VAR 2",
         "Id":"67-V2"
      },
      {  
         "ItemIds":[  
            "zzz"
         ],
         "Items":[  
            {  
               "Id":"67-V3",
               "GuId":"zzz",
               "Type":"Unit",
               "Status":"Active"
            }
         ],
         "Name":"VAR 3",
         "Id":"67-V3"
      }
   ],
   "ItemIds":[  

   ],
   "Items":[  

   ],
   "Name":"MAINP",
   "Id":"67",
   "Color":null
}

Here is my code, but it does not seem to work (the replacement part):

var jsonData = pm.response.json();

function replaceStatus() {
    _.each(jsonData.Variations, (arrayItem) => {
        if(arrayItem.Items.Status !== "NonActive") {

            arrayItem.Items.Status == "NonActive";
            console.log("arrayItem " + arrayItem);
        }
    });
}

pm.test("Run Function", replaceStatus ());

pm.sendRequest({
    url: 'localhost:3000/post',
    method: 'POST',
    headers: {
        "Content-Type": "application/json"
    },
    body: {
        mode: 'raw',
        raw: JSON.stringify(jsonData)
    }
}, (err, res) => {
    console.log(res)
})

回答1:


I guess you are trying to replace all NonActive values with Active. In that case, you should use = for assignment not ==. The JSON file you provided is not valid and couldn't run your code on my machine. I am happy to have a closer look if that didn't work

Based on your updates these changes need to be made

1- in order to deal with JSON object you need to parse the response as it is string and you can't call sth like JsonData.Variations on that.make sure jsonData is a JSON object. if not add sth like this to parse it

var parsedJson = JSON.parse(jsonData)

2- It seems that you missed one array layer in your function to iterate over items. As you have two nested arrays to reach Status the replaceStatus function should be as below

function replaceStatus() {
    _.each(parsedJson.Variations, (arrayItem) => {
        _.each(arrayItem.Items, (item) => {
            if(item.Status !== "NonActive") {
                item.Status = "NonActive";
                console.log("arrayItem " + item.Status);
            }
        });
    });
}



回答2:


Have you posted your entire code in the tests section or only a part of it? I saw from one of your comments that you cannot see the output logged to the console.

This may be very trivial, but, if you did post your entire code, it looks like you may have forgotten to call your replaceStatus() function before your post call.



来源:https://stackoverflow.com/questions/57298735/postman-how-to-replace-values-for-all-specific-properties-in-the-json-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!