Hello,
I have a problem. What I want to do is to obtain a token from RESTFul request, and parse that token to HTTP Header.
Here is my code
import internal.GlobalVariable as GlobalVariable
import com.kms.katalon.core.testobject.RequestObject
'Get response'
def response = WS.sendRequest(findTestObject('API/Authentication'))
'Verify if login was successfully'
WS.verifyElementPropertyValue(response, 'msg.ok[0]', 'Login successfull!')
def slurper = new groovy.json.JsonSlurper()
def result = slurper.parseText(response.getResponseBodyContent())
'Get token'
def token = result.data."X-Auth-Token"
'Print token'
println(token)
RequestObject ro2 = new RequestObject("y")
ro2.setHttpBody("id": "ab8e52e92deb459596e3911f9f09934c")
ro2.setHttpHeaderProperties("X-Auth-Token" : token)
ro2.setRestRequestMethod("POST")
ro2.setRestUrl("http://localhost/api/projects/scopeTo/")
WS.sendRequest(ro2)
1. I’m authenticating.
2. Verify whether there is a message in the response (Login successfull!)
3. Parse response with JSONSlurper
4. Save token to variable token
Now, when I have a token, I want to authenticate to another endpoint. What I need to do is to send in body an extra ID, and send in HTTP Header “X-Auth-Token” with token from variable.
Response from console:
02-19-2018 07:40:48 AM - [START] - Start action : Statement - println(token)
ojear3mndj1sarskbjl1r64dd5
02-19-2018 07:40:48 AM - [END] - End action : Statement - println(token)
02-19-2018 07:40:48 AM - [START] - Start action : Statement - ro2 = new com.kms.katalon.core.testobject.RequestObject(y)
02-19-2018 07:40:48 AM - [END] - End action : Statement - ro2 = new com.kms.katalon.core.testobject.RequestObject(y)
02-19-2018 07:40:48 AM - [START] - Start action : Statement - ro2.setHttpBody(["id":"ab8e52e92deb459596e3911f9f09934c"])
02-19-2018 07:40:48 AM - [END] - End action : Statement - ro2.setHttpBody(["id":"ab8e52e92deb459596e3911f9f09934c"])
02-19-2018 07:40:48 AM - [ERROR] - Test Cases/API/Authentication FAILED because (of) groovy.lang.MissingMethodException: No signature of method: com.kms.katalon.core.testobject.RequestObject.setHttpBody() is applicable for argument types: (java.util.LinkedHashMap) values: [[id:ab8e52e92deb459596e3911f9f09934c]]
Possible solutions: setHttpBody(java.lang.String), getHttpBody(), setSoapBody(java.lang.String), getSoapBody()
How should be that parsed and send to HTTP Header?
The error you got from the console is: ro2.setHttpBody(“id”: “ab8e52e92deb459596e3911f9f09934c”) since the function requires a String. I am not sure how the API is setup, you could try to send it as a String, ie “id:ab8e52e92deb459596e3911f9f09934c” instead.
Header name: X-OAPI-Application-Id
Header value ‘1111’ ( I will then change it by a variable later)
In this statement I give the name and the value of header
HTTPHeader.add(new TestObjectProperty(‘X-OAPI-Application-Id’, ConditionType.EQUALS, ‘1111’))
Based on POST method sign-in API i would like to pass my response(access token & token type) of sign-in API into authentication of next API
i would like to pass it in a script mode not manually , i have declared the response as global variable
access token in dynamic every time i hit sign-in API access token changes
so how to set the dynamic acess token and token type(Bearer) as http header in script mode please help