Post Request using RestRequestObjectBuilder

Is it possible to create an object in Katalon with the type of the POST request specified by the server reference, the basic authentication and the filled header without the body.
Please provide some example.

Thanks

Ibus said:

https://docs.katalon.com/katalon-studio/docs/web-services-builder.html

TestObjectProperty cmAuth3 = new TestObjectProperty(“Authorization”, ConditionType.EQUALS, cmToken3)

TestObjectProperty cType3 = new TestObjectProperty(‘Content-Type’, ConditionType.EQUALS, ‘text/plain’)

ArrayList headers3 = Arrays.asList(cmAuth3, cType3)

def url3 = ‘http://localhost:8080/adwiser/siva/cm/adv/campaign/state/’ + campid + ‘?depl=’ + ‘deploy’

println url3

RequestObject updCampState = new RestRequestObjectBuilder()

.withRestUrl(url3)

.withRestRequestMethod(‘POST’)

.withHttpHeaders(headers3)

.build()

def updCampStateRes = WS.sendRequest(updCampState)

WS.verifyResponseStatusCode(updCampStateRes, 200)

I m getting null pointer exception…Please help me to resolve my issue.

https://www.tutorialspoint.com/java/

https://www.tutorialspoint.com/groovy/

Ibus said:

https://www.tutorialspoint.com/java/

https://www.tutorialspoint.com/groovy/

for what???

well … you posted some incomplete codes, without an error log, and expect magic help.
i am sorry but i think nobody here posses such powers to figure out what is wrong without right info.

Ibus said:

well … you posted some incomplete codes, without an error log, and expect magic help.
i am sorry but i think nobody here posses such powers to figure out what is wrong without right info.

http://forum.katalon.com/discussion/9337/tip-my-test-is-broken-nothing-works-please-help-me

then u need to ask with me wat u want to verify what error and why but u send me link to learn to java and groovy…whats wrong with u man???

so post the detailed log with error if you expect help, otherwise you have to figure it out by yourself …

L.E: the full content of the script (perhaps formatted as code) may help too …

Ibus said:

so post the detailed log with error if you expect help, otherwise you have to figure it out by yourself …

L.E: the full content of the script (perhaps formatted as code) may help too …

url = http://localhost:8080/adwiser/siva/cm/adv/campaign/state/452?depl=undeploy
Method = POST

Without body its working fine in **POSTMAN. **But if i try execute samething in katalon studio i m getting error as bad request i.e 400 error code.

Script

def authdata = findTestData(‘ChangePass’)

GlobalVariable.CMUserName = authdata.getObjectValue(‘username’, 1)

GlobalVariable.CMPassword = authdata.getObjectValue(‘password’, 1)

def bAuth3 = authdata.getObjectValue(‘password’, 1)

byte[] decoded3 = bAuth3.decodeBase64()

String cmToken3 = WS.callTestCase(findTestCase(‘CMUserAuthTC’), [(‘Username’) : authdata.getObjectValue(‘username’, 1), (‘Password’) : new String(decoded3)], FailureHandling.STOP_ON_FAILURE)

TestObjectProperty cmAuth3 = new TestObjectProperty(“Authorization”, ConditionType.EQUALS, cmToken3)
ArrayList headers3 = Arrays.asList(cmAuth3)

class Data{

}

Data data = new Data()

String body = JsonOutput.toJson(data)

RequestObject reqObj = new RequestObject(“SetCampState”)

reqObj.setRestRequestMethod(“POST”)

reqObj.setRestUrl(“http://localhost:8080/adwiser/siva/cm/adv/campaign/state/452?depl=deploy”)

reqObj.setHttpHeaderProperties(headers3)

reqObj.setBodyContent(new HttpTextBodyContent(body, ‘UTF-8’, ‘text/plain’))

def response = WS.sendRequest(reqObj)

WS.verifyResponseStatusCode(response, 200)

**CONSOLE
**11-01-2018 09:51:22 AM - [INFO] - Checking response object

11-01-2018 09:51:22 AM - [FAILED] - Expected status code is ‘200’ but actual status code is ‘400’

11-01-2018 09:51:22 AM - [FAILED] - Unable to verify response status code (Root cause: Expected status code is ‘200’ but actual status code is ‘400’)

11-01-2018 09:51:22 AM - [END] - End action : verifyResponseStatusCode

11-01-2018 09:51:22 AM - [FAILED] - Test Cases/UpdCampStateTC FAILED because (of) (Stack trace: com.kms.katalon.core.exception.StepFailedException: Unable to verify response status code (Root cause: Expected status code is ‘200’ but actual status code is ‘400’)

at com.kms.katalon.core.keyword.internal.KeywordMain.stepFailed(KeywordMain.groovy:36)

at com.kms.katalon.core.keyword.internal.KeywordMain.stepFailed(KeywordMain.groovy)

at com.kms.katalon.core.keyword.internal.KeywordMain.runKeyword(KeywordMain.groovy:56)

at com.kms.katalon.core.webservice.keyword.builtin.VerifyResponseStatusCodeKeyword.verifyResponseStatusCode(VerifyResponseStatusCodeKeyword.groovy:37)

at com.kms.katalon.core.webservice.keyword.builtin.VerifyResponseStatusCodeKeyword.execute(VerifyResponseStatusCodeKeyword.groovy:32)

at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:53)

at com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords.verifyResponseStatusCode(WSBuiltInKeywords.groovy:212)

at com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords$verifyResponseStatusCode$1.call(Unknown Source)

at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)

at Script1540895385515.run(Script1540895385515.groovy:74)

at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:183)

at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:108)

at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:294)

at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:285)

at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:264)

at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:256)

at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:200)

at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:99)

at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:90)

at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)

at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:149)

at TempTestCase1541046076556.run(TempTestCase1541046076556.groovy:22)

at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:263)

at groovy.lang.GroovyShell.run(GroovyShell.java:518)

at groovy.lang.GroovyShell.run(GroovyShell.java:507)

at groovy.ui.GroovyMain.processOnce(GroovyMain.java:653)

at groovy.ui.GroovyMain.run(GroovyMain.java:384)

at groovy.ui.GroovyMain.process(GroovyMain.java:370)

at groovy.ui.GroovyMain.processArgs(GroovyMain.java:129)

at groovy.ui.GroovyMain.main(GroovyMain.java:109)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:109)

at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:131)

)

11-01-2018 09:51:22 AM - [END] - End Test Case : Test Cases/UpdCampStateTC

11.JPG

String cmToken3 = WS.callTestCase(findTestCase('CMUserAuthTC'), [('Username') : authdata.getObjectValue('username', 1), ('Password') : new String(decoded3)], FailureHandling.STOP_ON_FAILURE)TestObjectProperty cmAuth3 = new TestObjectProperty("Authorization", ConditionType.EQUALS, cmToken3)

Are you sure that you want it as header parametr?

assuming the other variables are properly initialized (tocken, user pass and so on, you can always put some println() in your code to watch them), looking at the following line:

reqObj.setBodyContent(new HttpTextBodyContent(body, 'UTF-8', 'text/plain'))

I cannot find it documented in the API doc.

Looks like you need to pass a json body, so try to use setHttpBody(java.lang.String)

https://api-docs.katalon.com/studio/v4.6.0.2/api/com/kms/katalon/core/testobject/RequestObject.html#setHttpBody(java.lang.String)

(make sure the body variable is actually a string containing your json data, and the json structure is properly formatted))

xio said:

String cmToken3 = WS.callTestCase(findTestCase('CMUserAuthTC'), [('Username') : authdata.getObjectValue('username', 1), ('Password') : new String(decoded3)], FailureHandling.STOP_ON_FAILURE)TestObjectProperty cmAuth3 = new TestObjectProperty("Authorization", ConditionType.EQUALS, cmToken3)

Are you sure that you want it as header parametr?

i need to pass empty body in post method. eventhough i tired to create empty json i sent in setHttpBody but i m getting as bad request.

Ibus said:

assuming the other variables are properly initialized (tocken, user pass and so on, you can always put some println() in your code to watch them), looking at the following line:

reqObj.setBodyContent(new HttpTextBodyContent(body, 'UTF-8', 'text/plain'))

I cannot find it documented in the API doc.

Looks like you need to pass a json body, so try to use setHttpBody(java.lang.String)

https://api-docs.katalon.com/studio/v4.6.0.2/api/com/kms/katalon/core/testobject/RequestObject.html#setHttpBody(java.lang.String)

(make sure the body variable is actually a string containing your json data, and the json structure is properly formatted))

Okay…
here i need to send empty json or empty body only. Eventhough i send empty json or empty string but i m getting only bad request.

There are so many items that can contribute to the errors you get.

I have no clue about what your api is expecting (400 usually is returned when a bad request is sent). Seems like you are initializing an empty json for the body, but you set the content as “text/plain” … that I cannot understand.

To debug it, you can always print to console various properties of the requestObject before actually sending it, e.g:

println(reqOb.getBodyContent())
...

an so on for header and other properties

However, since your testcase seems to be a bit complicated, wouldn’t be easier if you take another approach?

Just create an Web Request in the repository using the gui Object Builder, make sure it is working properly from the provided tool. Then, parametrize the variables you need (token, url and so on) and simply call it from the testcase, kind of:

request = findTestObject('YourRequestObject', [ ('url') : reqUrl, ('body') : reqBody, ('token') : yourToken ])
response = WS.sendRequest(request)

see the following for guidance:

And if you want to create RequestObject manualy read this useful article

https://docs.katalon.com/katalon-studio/tutorials/create_rest_api_requests_manually.html#responseobject