How to check for string in Restful API response

I wanted to check for the presence of a string in Restful API response.

I did this

def response = WS.sendRequest(findTestObject('Get Status'))

responseText = response.getResponseText()

WS.containsString(response, “completed”, true)


I got the following error

Test Cases/Get Status FAILED because (of) Unable to check contain string (Root cause: Given string ‘completed’ not found in response text
‘{“result”:{“id”:2,“status”:“completed”},“status”:“success”,“code”:200}’)

 

I can see that response text contains the word “completed”.

Can you help?

Please use assert response.getResponseText().contains("completed"). There is an issue in WS.containsString keyword.

Thanks.

2 Likes

duyluong said:

Please use assert response.getResponseText().contains("completed"). There is an issue in WS.containsString keyword.

Thanks.

hi duyluong, i have similar case

from case at the top,
how can i show response to console
i’m just want to show status":“success”

Thanks

Joko T. Susilo Widodo said:

duyluong said:

Please use assert response.getResponseText().contains("completed"). There is an issue in WS.containsString keyword.

Thanks.

hi duyluong, i have similar case

from case at the top,
how can i show response to console
i’m just want to show status":“success”

Thanks

Hi Joko,
Please take a look at builtin function JsonSurpler of groovy.
There were many similar discussions such as:



In the above mentioned case, please try:

def jsonSlurper = new JsonSlurper()def object = jsonSlurper.parseText(response. getResponseText())println object.statusassert object.status == 'success'