首页 > 解决方案 > 空手道 API:忽略“?” 在网址中

问题描述

我正在使用 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

标签: apiframeworkskarate

解决方案


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

当 a 中有特殊字符时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


推荐阅读