首页 > 解决方案 > 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 手动点击它并且它有效。

标签: groovyhttp-headerskatalon-studio

解决方案


除了只有授权,尝试添加内容类型并接受完整的标题。

例如 :

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
 }

参考链接


推荐阅读