【问题标题】:Katalon add authorization header failsKatalon 添加授权标头失败
【发布时间】:2019-01-17 21:06:23
【问题描述】:

我正在尝试通过代码将授权标头放在我的网络请求中。 授权值通过登录响应获取。

以下是我创建登录请求的方式:

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。它看起来像这样:

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 手动点击它,它可以工作。

【问题讨论】:

  • 定义propTokenresponseParsed.token的类型是什么?尝试使用responseParsed.token.toString()
  • 你能显示findTestObject('Object Repository/REST_Cicil/Hit Email Verification API - Staging Main')吗?

标签: groovy http-headers katalon-studio


【解决方案1】:

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

例如:

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
 }

Reference Link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多