groovy - Katalon 添加授权标头失败
问题描述
我正在尝试通过代码在我的网络请求中放置授权标头。授权值通过登录响应获取。
以下是我创建登录请求的方式:
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
接下来,我想访问另一个端点,使用标头作为 Authorization ,其值来自responseParsed.token
. 它看起来像这样:
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 手动点击它并且它有效。
解决方案
除了只有授权,尝试添加内容类型并接受完整的标题。
例如 :
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
}
推荐阅读
- android - 即使在 kill-server 之后模拟器运行时,adb 也会重新生成
- javascript - 如何从范围滑块添加两个值以形成单个值
- ruby-on-rails - 为什么我的 Rails 应用程序在推送到 Heroku 时无法工作:“ActionView::Template::Error (undefined method `toilet_available' for nil:NilClass)”?
- php - 使用 IF 语句映射 Woocommerce 订单状态
- android - 忽略 json 中的元信息
- javascript - 如何根据当前悬停的元素隐藏和显示元素?
- java - 如何将变量声明为常量数组的元素?
- postgresql - 修复损坏的表
- java - 将受保护的“卡片”数组元素从子类传递到子类
- django - Django - 检查用户是否对每个 url 进行了身份验证