Making PUT and GET Responses


#1

Hello guys,

I’m new in working with RESTful APIs. For my case I want to make an PUT Request and then GET it.
I made an PUT Request and it worked.

But now I want to make an GET Request. Can I somehow make so that GET Request will automatically take PUT Requests statementId and bind it to the link.

.
I’m really new for making such things and sorry if I asked a dumb question.


#2

Hello,

have a look here -

As soon as you are able to get guid from the response, it is easy to put it into next request URI.

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

// here comes GUID from your parsed response
String myGuid = UUID.randomUUID()

RequestObject ro = findTestObject("Misc/WStest")
ro.setRestUrl(String.format(ro.getRestUrl(), myGuid))

ResponseObject resp = WS.sendRequest(ro)

Your URI should look like this:

// %s instead of GUID
https://mysite.com/getRequest/%s

#3

Okay Marek, I will review that and will try to make that.


#4

How should i change URL ? my ULR looks like that http://test-api.com/v1/statement/1, last 1 is the accountId. and I’m making Put request then I should make GET Request.

EDIT: Oh, I had written myGuid second time wrong my bad. Case was executed well, but where did output go ? Also I dont get it now how to make GET Request, sorry for so many questions.


#5

Well, I used your link to parse Json. Now my code looks like that

I was trying to configure GET Request now, when I have id in idValue2. I used GET Requests link so :
http://testapi.com/v1/statement/idValue2


API GET Request with database
#6

Hello,

I see that you should have your GUID stored in idValue2 string.
Now, as I wrote in previous post, it is time to put it into next request’s URL.

Open your GET_request object and update your URL.
If your URL should look like this:
http://testapi.com/v1/statement/idValue2
Set it this way:
http://testapi.com/v1/statement/%s

Why? See:

RequestObject ro = findTestObject("GET_request")
ro.setRestUrl(String.format(ro.getRestUrl(), idValue2))

ResponseObject resp = WS.sendRequest(ro)

What does happen there?
Using setRestUrl(…), you can update your existing request object to include dynamic GUID (idValue2). And that’s it. :wink:

Note: You won’t be able to use GET_request test object without setting GUID in the test case as it contains only %s wildcard, not actual GUID.


#7

Hello Marek, Happy new year !

I made everything as you said, but how can I check that code works well ?

RequestObject rog = findTestObject(“GET_Request”)

rog.setRestUrl(String.format(rog.getRestUrl(), idValue2, idValue3, idValue4))

How should I see that ? and is there any way to make it easier ? Can it be modified with variables ?

image

Edit: When I try to verify my GET_Request it gives me error. Unable to verify response status code (Root cause: com.kms.katalon.core.exception.StepFailedException: Expected status code is ‘200’ but actual status code is ‘422’) In get Request I have now %s and because of that it doesnt give me 200 status code.


#8

Hello,

what are the other values in String.format method? Did you add any other wildcards into your URL?
Could you send a screenshot of your whole script?


#9

This is my code:


and this is my GET_Request:

and the url should look like that http://testapi.com/v1/statement/fff890e2-7a60-47b1-b914-db8434ce5c31?startPointer=0094230492304923090Anffjle39440583490434rieklfrlfkjedtu8er9t7548684568t9fkgmndflkgr5908yt546&count=100&inEquivalent=false


#10

You must use also other wildcards for all your variables.

http://testapi.com/v1/statement/%s?startPointer=%s&count=%s&inEquivalent=false

More about String.format method -


#11

Thanks Marek. Only one question, now when I have that link where can i check the output ? I see that I cant run it from GET_Request because it doesnt recognizes %s

But from test case it works well, and now status is 200. But I can’t see the output


#12

Well, my code would work only in a test case. I don’t use Verification in WS request at all.

If you want to see a response in a test case, you can use ResponseObject’s methods to get response text, status etc.

I can have a look at Verification in WS request later, it would be similar.


#13

Thank you for helping me.


#14

I think, that I found what i needed with your link about ResponseObjects.
def bo = WS.sendRequestAndVerify(rog).getResponseBodyContent()
System.out.println(bo)

And it prints as raw text in console, so now I can check that everything is working correctly.


#15

I’d recommend to create a response object and pick required information afterwards.

ResponseObject bo = WS.sendRequestAndVerify(rog)
String bodyContent = bo.getResponseBodyContent()
int statusCode = bo.getStatusCode()
// and whatever else you need
System.out.println(statusCode + ": " + bodyContent)

You can also create a condition if statusCode == 200 etc.


#16

Thats much better way ! Can I somehow make a pretty view in console for bodyContent ?

According to this Site it is possible.
https://www.toolsqa.com/rest-assured/read-json-response-body-using-rest-assured/


#17

Use org.json library. It contains many useful classes for working with JSON.

https://mvnrepository.com/artifact/org.json/json

Code:

import org.json.JSONObject

String jString = ''' {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "Sincere@april.biz",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874",
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
      }
    }
}'''

println new JSONObject(jString).toString(4);

#18

image


#19

Everything in Java is case-sensitive.


#20

I just added java-json.jre and library works now, but anyway i can’t print my respnse body content as pretty view

println new JsonObject(bodyContent).toString(4), I know I’m doing it wrong.