【问题标题】:How to provide parameter along with empty square brackets as param in GET method?如何在 GET 方法中提供参数以及空方括号作为参数?
【发布时间】:2023-02-07 19:54:53
【问题描述】:

我在将参数提供给 GET 方法时遇到了问题。 我有下一个网址

https://test.com/api/v1/account/?include[]=tickers&include[]=holdings&include[]=views

在我的示例中,如何在 GET 方法中提供参数以及空方括号作为参数? 我看到了https://github.com/karatelabs/karate/blob/master/karate-demo/src/test/java/demo/search/dynamic-params.feature,但没有找到我的解决方案(

【问题讨论】:

    标签: karate


    【解决方案1】:

    如果您看到 Karate 将其编码为 %5B%5D,那实际上是正确的行为:https://stackoverflow.com/a/59977660/143475

    您实际上可以在httpbin.org 上尝试此操作,并亲眼看看服务器是否能够正确解码它。如果您的服务器不能,则很可能是错误。

    * url 'https://httpbin.org/anything'
    * param include[] = 'tickers'
    * method get
    

    查看服务器响应:

    1 > GET https://httpbin.org/anything?include%5B%5D=tickers
    1 > Host: httpbin.org
    1 > Connection: Keep-Alive
    1 > User-Agent: Apache-HttpClient/4.5.14 (Java/17.0.6)
    1 > Accept-Encoding: gzip,deflate
    
    
    17:17:10.925 [main]  DEBUG com.intuit.karate - response time in milliseconds: 1528
    1 < 200
    1 < Date: Tue, 07 Feb 2023 11:47:10 GMT
    1 < Content-Type: application/json
    1 < Content-Length: 436
    1 < Connection: keep-alive
    1 < Server: gunicorn/19.9.0
    1 < Access-Control-Allow-Origin: *
    1 < Access-Control-Allow-Credentials: true
    {
      "args": {
        "include[]": "tickers"
      }, 
      "data": "", 
      "files": {}, 
      "form": {}, 
      "headers": {
        "Accept-Encoding": "gzip,deflate", 
        "Host": "httpbin.org", 
        "User-Agent": "Apache-HttpClient/4.5.14 (Java/17.0.6)", 
        "X-Amzn-Trace-Id": "Root=1-63e23a3e-4004ea8e65dba95003ec150e"
      }, 
      "json": null, 
      "method": "GET", 
      "url": "https://httpbin.org/anything?include[]=tickers"
    }
    

    那就是说您可以将所有内容移动到 URL:

    * url `https://httpbin.org/anything?include[]=tickers`
    * method get
    

    如果你仍然需要参数化,你可以看:https://stackoverflow.com/a/75061809/143475

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-05
      • 1970-01-01
      相关资源
      最近更新 更多