# Capture Response id from "PUT" REST API

**URL:** <https://forum.katalon.com/t/capture-response-id-from-put-rest-api/16069>\
**Category:** API Testing\
**Tags:** api-testing, katalon-studio\
**Created:** [December 28, 2018, 5:46am UTC](https://forum.katalon.com/t/capture-response-id-from-put-rest-api/16069 "2018-12-28T05:46:32Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Evan](https://avatars.discourse-cdn.com/v4/letter/e/54ee81/32.png) [@Evan](https://forum.katalon.com/u/Evan)\
**Post date:** [December 28, 2018, 5:46am UTC](https://forum.katalon.com/t/capture-response-id-from-put-rest-api/16069/1 "2018-12-28T05:46:32Z")

</div>

I have a REST API (PUT) and included the following on my script:

> // Create a new test execution instance  
> def create\_test\_execution = WS.sendRequest(findTestObject(‘JIRA/ZAPI-Update Execution Details’, [(‘projectId’) : GlobalVariable.JIRA\_Project\_ID  
> , (‘cycleId’) : ‘-1’, (‘issueId’) : json\_create\_issue.id  
> , (‘assigneeType’) : ‘asignee’, (‘asignee’) : ‘sysadmin’, (‘authorization’) : GlobalVariable.JIRA\_Authorisation, (‘url’) : GlobalVariable.JIRA\_URL]))
> 
> **def json\_create\_test\_execution = jsonSlurper.parseText(create\_test\_execution.getResponseText())**

It returns _java.lang.IllegalArgumentException: Text must not be null or empty_

I am trying to captue the response details such as the JIRA ID.

When I ran the script (not via Test case) but via the API request and Verify method, the response shows 200 and the specific ID.

---

<div class="post-metadata">

**Author:** ![Brian\_Ducson](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/brian_ducson/32/603_2.png) [@Brian\_Ducson](https://forum.katalon.com/u/Brian_Ducson)\
**Post date:** [December 28, 2018, 7:10am UTC](https://forum.katalon.com/t/capture-response-id-from-put-rest-api/16069/2 "2018-12-28T07:10:31Z")

</div>

What is the response code if you send the request via script?

create\_test\_execution.getStatusCode()

---

<div class="post-metadata">

**Author:** ![Evan](https://avatars.discourse-cdn.com/v4/letter/e/54ee81/32.png) [@Evan](https://forum.katalon.com/u/Evan)\
**Post date:** [December 28, 2018, 7:55am UTC](https://forum.katalon.com/t/capture-response-id-from-put-rest-api/16069/3 "2018-12-28T07:55:28Z")

</div>

The status is 200. It just doesn’t seem to capture the details of the Response text using the PUT method. It works for POST method which I used for another functionality of creating a JIRA test case:

> ```
> // Create a new test case with provided information - this needs to be done once		
> def create_issue = WS.sendRequest(findTestObject('JIRA/Create Issue Type', [('projectKey') : GlobalVariable.JIRA_Project_Key
> , ('summary') : currentTestCaseName, ('description') : 'Creating of an issue using project keys and issue type names using the REST API'
> , ('issueType') : 'Test', ('priority') : 'Major', ('authorization') : GlobalVariable.JIRA_Authorisation, ('url') : GlobalVariable.JIRA_URL]))
>   
> **def json_create_issue = jsonSlurper.parseText(create_issue.getResponseText())**
> 
> ```

---

<div class="post-metadata">

**Author:** ![Brian\_Ducson](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/brian_ducson/32/603_2.png) [@Brian\_Ducson](https://forum.katalon.com/u/Brian_Ducson)\
**Post date:** [December 28, 2018, 9:55am UTC](https://forum.katalon.com/t/capture-response-id-from-put-rest-api/16069/4 "2018-12-28T09:55:36Z")

</div>

Exactly which Jira API you are using? Could you take a screenshot of the expected result?

---

<div class="post-metadata">

**Author:** ![Evan](https://avatars.discourse-cdn.com/v4/letter/e/54ee81/32.png) [@Evan](https://forum.katalon.com/u/Evan)\
**Post date:** [December 28, 2018, 1:21pm UTC](https://forum.katalon.com/t/capture-response-id-from-put-rest-api/16069/5 "2018-12-28T13:21:33Z")

</div>

I am actually using ZAPI (Zephyr API plug-in) for JIRA. So I was using the REST API below:

> **[Zephyr for JIRA API · Apiary](https://getzephyr.docs.apiary.io/#reference/executionresource/update-execution-details/update-execution-details)**
>
> A place where APIs are kept.

When I run it via the Web object screen (Request and Verify), it shows exactly what I needed (with status 200) similar to the below:

> {  
> “id”: 49,  
> “orderId”: 21,  
> “executionStatus”: “3”,  
> “executedOn”: “Today 2:10 PM”,  
> “executedBy”: “vm\_admin”,  
> “executedByDisplay”: “vm\_admin”,  
> “comment”: “”,  
> “htmlComment”: “”,  
> “cycleId”: 51,  
> “cycleName”: “c1”,  
> “versionId”: -1,  
> “versionName”: “Unscheduled”,  
> “projectId”: 10100,  
> “createdBy”: “vm\_admin”,  
> “modifiedBy”: “vm\_admin”,  
> “issueId”: 10400,  
> “issueKey”: “HTC-1”,  
> “summary”: “test1”,  
> “label”: “”,  
> “component”: “”,  
> “projectKey”: “HTC”  
> }

In the Response above, I’d like to get the value of “ **id** ” which is 49.

However, the code below throws me an error that the Text is null or empty:

> def json\_create\_test\_execution = jsonSlurper.parseText(create\_test\_execution.getResponseText())  
> String testId = json\_create\_test\_execution.id

---

<div class="post-metadata">

**Author:** ![Evan](https://avatars.discourse-cdn.com/v4/letter/e/54ee81/32.png) [@Evan](https://forum.katalon.com/u/Evan)\
**Post date:** [December 28, 2018, 2:40pm UTC](https://forum.katalon.com/t/capture-response-id-from-put-rest-api/16069/6 "2018-12-28T14:40:45Z")

</div>

Ok I got it. I mistakenly (or stup\*dly) was calling the incorrect API. Thanks for the effort looking into it though @Brian_Ducson

---

<div class="post-metadata">

**Author:** ![Evan](https://avatars.discourse-cdn.com/v4/letter/e/54ee81/32.png) [@Evan](https://forum.katalon.com/u/Evan)\
**Post date:** [December 29, 2018, 2:25pm UTC](https://forum.katalon.com/t/capture-response-id-from-put-rest-api/16069/7 "2018-12-29T14:25:11Z")

</div>

@Brian_Ducson Ok I got another problem (which still applies to the original topic I asked earlier) as it is always showing null when I am trying to capture a value from the response body.  
So, the response body generated is as follows:

> {  
> “39”: {  
> “id”: 39,  
> “name”: Test  
> }  
> }

However, when I am trying to display the “id” value using the jsonSlurper, it returns null.

> def response\_to\_print = jsonSlurper.parseText(.getResponseText())  
> log.logInfo(response\_to\_print.toString()) **→ shows correct as {39={id=39, name=Test}}**  
> log.logInfo(response\_to\_print.id) **→ shows null**

Note that “39” is dynamic and not fixed so it keeps changing everytime. As I need to pass this value to test case, then I can’t just hardcode it to “39” so I need to capture the value of “ **id** ” or I workaround by using substring to grabe the values before “ **: {** ”

I am fairly new to automation of API so bare with me if the terms I use are not correct.

---

<div class="post-metadata">

**Author:** ![Zarashima](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/zarashima/32/928_2.png) [@Zarashima](https://forum.katalon.com/u/Zarashima)\
**Post date:** [December 31, 2018, 2:33am UTC](https://forum.katalon.com/t/capture-response-id-from-put-rest-api/16069/8 "2018-12-31T02:33:09Z")

</div>

To reduce this behavior in the future, JSonPath is suggested to use. Similar to XPath or CSS locators of a WebElement, using JSonPath can locate the response’s node:  
[https://docs.katalon.com/katalon-studio/tutorials/verify-api-responses.html#verifying-rest-response-in-json-format](https://docs.katalon.com/katalon-studio/tutorials/verify-api-responses.html#verifying-rest-response-in-json-format)

---

<div class="post-metadata">

**Author:** ![Brian\_Ducson](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/brian_ducson/32/603_2.png) [@Brian\_Ducson](https://forum.katalon.com/u/Brian_Ducson)\
**Post date:** [December 31, 2018, 4:10am UTC](https://forum.katalon.com/t/capture-response-id-from-put-rest-api/16069/9 "2018-12-31T04:10:40Z")

</div>

import groovy.json.JsonSlurper

def jsonSlurper = new JsonSlurper()

def y = jsonSlurper.parseText(x)

def key = y.keySet()[0]

println y.get(key).get(id)

---

<div class="post-metadata">

**Author:** ![Evan](https://avatars.discourse-cdn.com/v4/letter/e/54ee81/32.png) [@Evan](https://forum.katalon.com/u/Evan)\
**Post date:** [January 1, 2019, 11:03pm UTC](https://forum.katalon.com/t/capture-response-id-from-put-rest-api/16069/10 "2019-01-01T23:03:00Z")

</div>

> [@Evan](#):
>
> 39

Hi Vinh, I use this most of the time but since the parent node is dynamic, I cannot pass this as a variable.  
the jSonPath will keep changing (e.g. 30.id then when I run the script again, the response will change to 31.id, 32.id, and so on and so forth).

---

<div class="post-metadata">

**Author:** ![Evan](https://avatars.discourse-cdn.com/v4/letter/e/54ee81/32.png) [@Evan](https://forum.katalon.com/u/Evan)\
**Post date:** [January 1, 2019, 11:20pm UTC](https://forum.katalon.com/t/capture-response-id-from-put-rest-api/16069/11 "2019-01-01T23:20:06Z")

</div>

Thanks Brian, thanks for this. I got it working using the below:

> def jsonSlurper = new JsonSlurper()  
> def create\_test\_execution = WS.sendRequest(findTestObject(‘…’)  
> def json\_create\_test\_execution = jsonSlurper.parseText(create\_test\_execution.getResponseText())
> 
> //skip the parent value in ZAPI as it is always dynamic  
> **def (rootKey, testExecutionId) = json\_create\_test\_execution.findResult { k, v → [k, v.id] }**

---

<div class="post-metadata">

**Author:** ![Evan](https://avatars.discourse-cdn.com/v4/letter/e/54ee81/32.png) [@Evan](https://forum.katalon.com/u/Evan)\
**Post date:** [January 4, 2019, 1:10am UTC](https://forum.katalon.com/t/capture-response-id-from-put-rest-api/16069/12 "2019-01-04T01:10:33Z")

</div>

Hi @Brian_Ducson , would you know how to skip two levels of dynamic values?  
I need to retrieve the 3rd level value by ignoring the parent level (which is always dynamic) and the 2nd level which is unfortunately, also dynamic. Response body below:

> {  
> “ **1** ”:[  
> {  
> “ **8** ”:{  
> “totalExecutions”:0,  
> }

From above, I need to skip “1” and “8” (both will dynamically change) before retrieving the value “0” of totalExecutions. I tried following the nested loop statement from here (putting another loop statement within the loop:  
[http://forum.katalon.com/t/how-to-get-json-value-skip-first-level/10857/6](http://forum.katalon.com/t/how-to-get-json-value-skip-first-level/10857/6)

but I get the error below:  
**_com.kms.katalon.core.exception.StepErrorException: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object ‘1=[{8={totalExecutions=0}]’ with class ‘java.util.TreeMap$Entry’ to class ‘java.util.Map’_**

---

<div class="post-metadata">

**Author:** ![Evan](https://avatars.discourse-cdn.com/v4/letter/e/54ee81/32.png) [@Evan](https://forum.katalon.com/u/Evan)\
**Post date:** [January 7, 2019, 10:19pm UTC](https://forum.katalon.com/t/capture-response-id-from-put-rest-api/16069/13 "2019-01-07T22:19:37Z")

</div>

@Zarashima any ideas on this? 🙂

---

<div class="post-metadata">

**Author:** ![huynguyen](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/huynguyen/32/552_2.png) [@huynguyen](https://forum.katalon.com/u/huynguyen)\
**Post date:** [January 7, 2019, 11:58pm UTC](https://forum.katalon.com/t/capture-response-id-from-put-rest-api/16069/14 "2019-01-07T23:58:24Z")

</div>

@Evan, can you provide your script?

---

<div class="post-metadata">

**Author:** ![Evan](https://avatars.discourse-cdn.com/v4/letter/e/54ee81/32.png) [@Evan](https://forum.katalon.com/u/Evan)\
**Post date:** [January 8, 2019, 6:21am UTC](https://forum.katalon.com/t/capture-response-id-from-put-rest-api/16069/15 "2019-01-08T06:21:03Z")

</div>

I was just checking the 2nd level at this stage, but getting the error already as it seems not able to identify the Map variable:

> def search\_test\_cycle = WS.sendRequest(findTestObject(‘JIRA/ZAPI-Get List of Cycle’, [(‘authorization’) : GlobalVariable.JIRA\_Authorisation, (‘projectId’) : GlobalVariable.JIRA\_Project\_ID  
> , (‘url’) : GlobalVariable.JIRA\_URL, (‘versionId’) : ‘’, (‘cycleId’) : ‘’, (‘issueId’) : ‘’])) **→ the response body for this is the above {“1”:[**{ **“8”:{** “ **totalExecutions”:0** }
> 
> def json\_search\_test\_cycle = jsonSlurper.parseText(search\_test\_cycle.getResponseText())  
> KeywordUtil.logInfo(json\_search\_test\_cycle.toString())  
> def d = json\_search\_test\_cycle.keySet()[0]
> 
> for (Map map : json\_search\_test\_cycle){  
> Set keySet = map.keySet()  
> Iterator iter = keySet.iterator()  
> while (iter.hasNext()) {  
> def key = iter.next()  
> def v = map.get(key)  
> KeywordUtil.logInfo(v.toString())  
> }  
> }

---

<div class="post-metadata">

**Author:** ![Evan](https://avatars.discourse-cdn.com/v4/letter/e/54ee81/32.png) [@Evan](https://forum.katalon.com/u/Evan)\
**Post date:** [January 14, 2019, 10:32pm UTC](https://forum.katalon.com/t/capture-response-id-from-put-rest-api/16069/16 "2019-01-14T22:32:18Z")

</div>

anyone? 🙂

---

<div class="post-metadata">

**Author:** ![devalex88](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/devalex88/32/336_2.png) [@devalex88](https://forum.katalon.com/u/devalex88)\
**Post date:** [April 18, 2019, 6:58am UTC](https://forum.katalon.com/t/capture-response-id-from-put-rest-api/16069/17 "2019-04-18T06:58:21Z")

</div>

> [@Evan](#):
>
> com.kms.katalon.core.exception.StepErrorException: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object ‘1=[{8={totalExecutions=0}]’ with class ‘java.util.TreeMap$Entry’ to class 'java.util.Map’

The object is not a `Map`, but a `Map.Entry`, i.e. a key-value pair (think of `Map` as a `Set` of key-value pairs). Let’s say we have a map `m`:

```auto
{
"1":[
{
"8":{
“totalExecutions”:0,
}

```

you can try

```auto
def firstLevel = m.firstEntry().getValue()
def secondLevel = firstLevel[0].firstEntry().getValue()

```

I didn’t try the code myself - if you encounter problems don’t hesitate to let me know.

See [TreeMap (Java Platform SE 8 )](https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html#firstEntry) for more details.
