简体   繁体   中英

In Postman, how do I take a response body from a Get Request, and put it in a PUT request with minor changes

I have ran a POST request to create a new contract. The Json response for this request was a string ID.

"03007"

My Post request has a test to save the response as an environment variable.

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("newContractNb", jsonData);

I then have a GET request that plugs the response variable into the request so that I Get the data for the contract I created.

在此处输入图片说明

My Response is like so.

{
    "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
}

I have a Test in the Get method to save this response body as a variable.

pm.environment.set('getRequestBody', pm.response.json())

Is it possible to then use this variable in the Body or Pre-req section of a PUT request, but somehow make it so that it alters a select parameter(s) - such as the contractPrtyNm only?

When you get the response from "{{url}}/{{newContractNb}}" you could try something like this. Modify the property then set the body in a request:

var jsonData = pm.response.json();
jsonData.progSrvcNm = "blah"; 

pm.environment.set("response_b", jsonData);

Then in your PUT request (the type shouldn't matter) body just set it with

pm.environment.get("response_b");

thanks @Danny Dainton for the help!

My Get method after the post contained the following in order to save the response as a variable and edit it all in one.

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))

I then ran my PUT method with the variable in the body.

{{responseEdit}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM