I have an API that I’m testing that requires that a bearer token be generated on each run as it has a short expiration. I am able to successfully use one web service request to generate the token and have it written to a variable, but I can’t seem to then use that string in the next API request I do. Currently I’m using the following line, but the second request isn’t receiving the token and responds with an error.
dev apiSecRes = WS.sendrequest(findTestObject('apiObjs/Health_Chk', [('Authorization') : "Bearer ${apiToken}"]))
I also have the variable name set in the Web Request object in my Object Repository. What am I missing?
Hi Katalon experts @trust_level_2,
Anyone able to help Sean with his question?
Thanks,
Albert
firstly get the token by firing the request as below
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import groovy.json.JsonSlurper
// 1. Send request to get the token
def tokenResponse = WS.sendRequest(findTestObject('apiObjs/Health_Chk'))
// 2. Parse the JSON response
def responseBody = tokenResponse.getResponseBodyContent()
def jsonSlurper = new JsonSlurper()
def jsonResponse = jsonSlurper.parseText(responseBody)
// 3. Extract the token from the JSON (key name may vary)
String bearerToken = jsonResponse.token // or jsonResponse.access_token, etc.
// 4. Store in a Global Variable for reuse
GlobalVariable.authToken = bearerToken
aftr the above , ass the Token via Script
def apiSecRes = WS.sendRequest(findTestObject(
'apiObjs/Health_Chk',
[
('Authorization'): "Bearer " + GlobalVariable.authToken
]
))
you can still improve the code but just to give you a hint