Forum Discussion

fazlook's avatar
fazlook
Frequent Contributor
8 years ago

How to get the REST PUT response from request1 and use it in another REST PUT request2

Say I have PUT reuqest1, it creates an ID for me in the  response in JSON format.

PUT request1

{

"name": "Soap",

"family": "UI"

}

response is

{

"id": "2",

"name": "Soap",

"family": "UI"

}

 

I would like to take the whole thing to send another PUT request with the "name" updated this time. Basically, i want to send reuqest2

{

"id": "2",

"name": "SoapSoap",

"family": "UI"

}

 

 

The problem is that i don't know the ID. It is arbitrary. 

  • So use Script assertion in my first put ? so this is the first Test Step ?

    Can I use that value in both Test Steps and Test Case levels ?

     

    **Update1**

    I worked the first step. Now I used this in my next step (a second PUT request to update the name)

     

    {
    "id": ${#TestCase1#ID}
    "name": "nameupdate",
    "updateBy": null
    } and got this message

     

    [{
    "context": null,
    "message": "Request body was not specified, or was improperly formatted and could not be deserialized."
    }]

     

    **Update2**

    I was missing a , at the end of "id": ${#TestCase1#ID}. It is now fixed, however, I have a new problem now. It is creating a new ID everytime I send the request again. It should update the name not creating a new id :( 

     

    **Update3**

    My test case name is : TestCase1 , so if I use "id": ${#TestCase1#ID} or "id": ${#Test1#ID} or "id": ${#Test#ID} , they are behaving the same as Update2

     

    rupert_anderson you might help as well ? :) 

     

    **Update4** 

    I needed to use quotation marks like that:  '${#TestCase#ID}' in the body of my next PUT request for this to work :)  

  • nmrao's avatar
    nmrao
    Champion Level 3
    So, you want to retrieve "id" from request1 to request2?
  • nmrao's avatar
    nmrao
    Champion Level 3
    By the way, is that how your actual request appears? If the data changes i.e., more complex, then code might have to be changed. May be you can provide sample resembling your data, but need to not be actual.
    • fazlook's avatar
      fazlook
      Frequent Contributor

      The first PUT request is right yes. Then the system by default will create/generate something like this :

       

      {
         "id": "8e8a8347-3b96-4de4-8b3d-0fe1e2d0efcb",
         "name": "Soap",
         "inactive": false,
         "insertDate": "2016-12-14T21:14:16",
         "updateBy": null,
         "updateDate": "2016-12-14T21:14:16",
         "family": "UI",
         "Phone": null,
         "Pager": null,
         "MobileNumber": null,
         "EmailAddress": null
       
      }

      Then I want to be able to take that Response (With the same generated ID) and use a PUT request on it to update the "name" and "family" and so on.........

      • nmrao's avatar
        nmrao
        Champion Level 3

        You can use the Script Assertion for the first step to retrieve the required data and store them into test case level properties and use them in any other subsequent test steps of the same test case using property expansion.

         

        You can get the response(which you have shown in the post) using context.response variable in Script Assertion.

        /** 
        * This is script assertion
        **/
        //Check if you have got the response.
        
        assert context.response, "Response is empty or null"
        
        
        //Create json object out of response
        
        def jsonObject = new groovy.json.JsonSlurper().parseText(context.response)
        
        
        //Get the id
        
        def idFromResponse= jsonObject.id
        
        
        //Check if the id has data
        
        assert idFromResponse, "id from response is empty or null"
        
        log.info "id from response is ${idFromResponse}"
        
        
        //Store the id value at test case level custom properties for later use
        
        context.testCase.setPropertyValue('ID', idFromResponse.toString())

        Similarly you can use it for other data as well.In the next Request step, the following change is required which uses property expansion:If the request in the following is:

         

        {

             "id": ${#TestCase#ID}

        }