[英]In Postman, how do I take a response body from a Get Request, and put it in a PUT request with minor changes
我已執行POST請求以創建新合同。 此請求的Json響應是字符串ID。
"03007"
我的發布請求具有將響應另存為環境變量的測試。
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("newContractNb", jsonData);
然后,我有一個GET請求,將響應變量插入到請求中,以便獲取我創建的合同的數據。
我的回應就是這樣。
{
"contractNb": "03007",
"progSrvcNm": "009",
"contractPrtyNm": "PostmanAutomationContract",
"contractCd": "000",
"signDt": "2018-01-01T00:00:00",
"startDt": "2018-01-01T00:00:00",
"endDt": "2025-01-01T00:00:00",
"remitTerms": 30
}
我在Get方法中有一個Test來將該響應主體保存為變量。
pm.environment.set('getRequestBody', pm.response.json())
然后可以在PUT請求的“正文”或“先決條件”部分中使用此變量,但是以某種方式使其能夠更改選擇參數(例如,僅限contractPrtyNm)嗎?
當您收到來自“ {{url}} / {{newContractNb}}”的回復時,您可以嘗試這樣的操作。 修改屬性,然后在請求中設置主體:
var jsonData = pm.response.json();
jsonData.progSrvcNm = "blah";
pm.environment.set("response_b", jsonData);
然后在您的PUT請求(類型無關緊要)中,將其設置為
pm.environment.get("response_b");
感謝@Danny Dainton的幫助!
帖子后的My Get方法包含以下內容,以便將響應另存為變量並將其全部編輯。
let jsonData = pm.response.json()
jsonData.stuff = "PostmanAutomationContractEdit"
jsonData.otherStuff = 45
jsonData.altId1 = "AutoEdit"
function thing(id, classification, foo, foos, barf, contactNb) {
this.id = id
this.classification = classification
this.foo = foo
this.foos = foos
this.barf = barf
this.contactNb = contactNb
}
let newThingData = new thing(pm.environment.get('newContractNb'), "bil", "y", "n", jsonData.thing[0].things, "000914")
jsonData.stuff[0].nextLevelStuff[0] = newPartyData
pm.environment.set('responseEdit', JSON.stringify(jsonData))
然后,我使用體內的變量運行PUT方法。
{{responseEdit}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.