【问题标题】:Karate API: Ignore the '?' in the url [duplicate]空手道 API:忽略“?”在网址中[重复]
【发布时间】:2021-10-26 10:50:08
【问题描述】:

我正在使用 Karate API 框架中的 path 关键字来发出请求。路径存储在从另一个特征文件返回的变量中。但是,我无法通过“?”。这是我正在做的事情:

我有一个“paths.feature”文件,可以获取我后续请求的所有路径。以下是返回路径的示例:

/LegalEntity/{legalEntityId}/taxidentifier/{Id}?formId=captureCustomerContact&subformid=taxIdentifierSimplified

然后我“替换”如下参数:

/LegalEntity/43/taxidentifier/13?formId=captureCustomerContact&subformid=taxIdentifierSimplified

然后我从一个新的功能文件中调用此功能文件并将存储的变量 (def) 传递到路径中,但是,URL 不会忽略“?”并且测试失败。任何指示或帮助将不胜感激。谢谢

错误

/LegalEntity/43/taxidentifier/13%3FformId=captureCustomerContact&subformid=taxIdentifierSimplified

【问题讨论】:

  • 嗨,彼得。我了解如何将数据从一个功能文件传递到另一个功能文件。这里的问题是,当我从其他功能调用“def”并将其作为“路径”传递时,它不接受“?”。我怎样才能将它作为路径传递? /LegalEntity/43/taxidentifier/13?formId=captureCustomerContact&subformid=taxIdentifierSimplified
  • 我只是说,accept PAST 回答堆栈溢出是一种很好的形式,仅此而已。再次阅读第二个链接。

标签: api frameworks karate


【解决方案1】:

我认为你很困惑,这与调用功能无关。

path 中有特殊字符时,它将被编码。这就是 HTTP 的工作原理。参考:https://stackoverflow.com/a/54477346/143475

尝试以下 3 行测试,看看会发生什么:

* url 'https://httpbin.org'
* path 'anything/?'
* method get

实际发送的 URL 为:https://httpbin.org/anything/%3F

但你要明白,当你正确使用param关键字时,?会自动插入:

* url 'https://httpbin.org'
* path 'anything'
* param foo = 'bar'
* method get

这里的 URL 是:https://httpbin.org/anything?foo=bar

所以你应该做的是重构你的测试以通过 formIdsubformid 作为查询参数,例如:

* def someVariable = 'captureCustomerContact'
* url 'https://httpbin.org'
* path 'anything'
* param formId = someVariable
* method get

请花一些时间来研究它是如何工作的。这里的 URL 将是 https://httpbin.org/anything?formId=captureCustomerContact,当然,您可以拥有更多 param 实例。请阅读文档:https://github.com/karatelabs/karate#param

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    相关资源
    最近更新 更多