How do I verify the response in an Restful API call?

I am making a Rest API POST call, the test shows the “Send Request Successfully” however I see no Method to handle the response of the request, and I also note that even if the API request is made with invalid values the log response is the same “Send Request Successfully” so this is actually not an indicator of a valid request ( a response of a 200 code) So…

How do I validate a API POST request and extract values from the response body ?

Where do I view the response from an API request?

I want to validate and log the response code and extract and use in further requests specific contents of the Body:

This is an example of the returned request (using soap ui ) I am looking to of course capture the “access_token”

{
“access_token”: “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”,
“token_type”: “Bearer”,
“expires_in”: 450,
“refresh_token”: “xxxxxxxxxxxxxxxxxxxxxxxx”,
“refresh_token_expires_in”: 900
}

Hi Mark Peterson,

For your concern:

Where do I view the response from an API request?

  • You could create a Web Service Request object from Object Repository and view the response using Test Request button in Web Service Request manual view.

How do I validate a API POST request and extract values from the response body?

  • To answer your question, please see an example below:
  1. Create new Web Service Request in Object Repository with

    1. Name = RESTful POST

    2. Request Type = RESTful

    3. Request method = POST

    4. Request URL = http://httpbin.org/post

    5. HTTP Headers

    6. Content-Type = application/json

    7. Accept-Charset = utf-8

    8. HTTP Body = { “username”: “John Doe”, “password”: “xxx” }

  2. Create new Test Case

    1. Name = Get Out-Going IP

    2. Script

responseData = WS.sendRequest(findTestObject('RESTful POST'))responseCode = responseData.getStatusCode()println('Response Code: ' + responseCode)// respone bodyresponseText = responseData.getResponseText()println('Response body: ' + responseText)def jsonObject = new groovy.json.JsonSlurper().parseText(responseText)// extract value from response body // get origin attributedef ip = jsonObject.originprintln('IP: ' + ip)

Run your test case and see the result.

Hi Nguyen,

I have followed your code and paste the below code to fetch the response

responseData = WS.sendRequest(findTestObject('RESTful POST'))responseCode = responseData.getStatusCode()println('Response Code: ' + responseCode)// respone bodyresponseText = responseData.getResponseText()println('Response body: ' + responseText)def jsonObject = new groovy.json.JsonSlurper().parseText(responseText)// extract value from response body // get origin attributedef ip = jsonObject.originprintln('IP: ' + ip)

but got the error. Invalid token error and test not run.

Now please guide me on my json response:

{
“status”:1,
“message”:“success”,
“result”:{
“id”:“2kxv1dvtovf-ab23ad18-cffe-46d4-bf83-4bb380560dea”,
“token”:“2w2c6ql8849-32fb9336-7995-47c4-9024-6a35c6043621”,
“country_code”:“91”,
“mobile_number”:“9091401966”,
“first_name”:“Dipak”,
“last_name”:“Das”,
“email”:"dipak.das@gmail.com",
“status”:0
},
“success”:true,
“hasError”:false
}

Now How can I fetch the token which is dynamic

Thank for your help in advance