我正在尝试通过代码在我的Web请求中放置授权标头。 授权值是通过登录响应获得的。
这是我创建登录请求的方式:
loginResponse = WS.sendRequest(loginRequest)
WS.verifyResponseStatusCode(loginResponse, 200)
println '>>> login status code is: ' + loginResponse.statusCode
并获得如下响应:
println '>>> response: ' + loginResponse.getResponseBodyContent()
String responseString = loginResponse.getResponseBodyContent()
JsonSlurper slurper = new JsonSlurper()
Map responseParsed = slurper.parseText(responseString)
println '>>> token: ' + responseParsed.token
接下来,我想使用标题为responseParsed.token
中的值的Authorization来达到另一个端点。看起来像这样:
RequestObject requestActivate = new RequestObject('activator')
TestObjectProperty propToken = new TestObjectProperty('Authorization', ConditionType.EQUALS, responseParsed.token)
ArrayList headers = Arrays.asList(propToken)
requestActivate.setHttpHeaderProperties(headers)
requestActivate.setRestUrl('http://xxxxxx.com/registration/activate_email')
requestActivate.setRestRequestMethod('GET')
verifResponse = WS.sendRequest(findTestObject('Object Repository/REST_Cicil/Hit Email Verification API - Staging Main'))
但是我总是会收到403 Forbidden
错误。好像我没有正确添加标题。我尝试使用Postman手动点击它,并且可以正常工作。
答案 0 :(得分:1)
除仅授权外,尝试添加content-type并接受为完整标头。
例如:
String endpoint = "https://www.katalon.com"
String requestMethod = "GET"
String authHeader = "whateverYouNeedForAuthentication"
TestObjectProperty header1 = new TestObjectProperty("Authorization", ConditionType.EQUALS, authHeader)
TestObjectProperty header2 = new TestObjectProperty("Content-Type", ConditionType.EQUALS, "application/json")
TestObjectProperty header3 = new TestObjectProperty("Accept", ConditionType.EQUALS, "application/json")
ArrayList defaultHeaders = Arrays.asList(header1, header2, header3)
public ResponseObject buildApiRequest1() {
RequestObject ro = new RequestObject("objectId")
ro.setRestUrl(endpoint)
ro.setHttpHeaderProperties(defaultHeaders)
ro.setRestRequestMethod(requestMethod)
ResponseObject respObj = WS.sendRequest(ro)
return respObj
}