【问题标题】:Send a vector using the R httr GET function's `query` parameter使用 R httr GET 函数的 `query` 参数发送向量
【发布时间】:2020-07-13 20:03:35
【问题描述】:

对于发送值向量,例如数组 list_a = c(1,2,3) FastAPI 将接受以下形式的 URL:

https://wherever.com/endpoint?list_a=1&list_a=2&list_a=3

但是,使用库 httr's query parameter 到 GET 函数,您必须传递一个键/值对列表。这意味着您不能两次拥有相同的字段,因为 R 显然不会接受具有重复键的列表。

那么我该怎么做呢?我可以自己构建 URL,但问题是我的一些参数中有双引号 ("),如果我将它们直接放入 url,似乎无法正确解析。不过,query 参数似乎可以正确处理这些问题。

有什么方法可以让httr的GETquery参数创建多个相同的字段名?

或者,我如何编码一个预先创建的包含双引号的 URL,如下所示,这样它就不会导致 FastAPI 出现 HTTP 错误?

"query/Crude/?actual_table_name=live.crude&report_id=xxxxxxx&fields=IMO&where={\"Barrels\":{\"gt\":1},\"conjunction\":\"\"}&where={\"Load Date\":{\"gt\":\"'2000-01-01'\"},\"conjunction\":\"\"}&offset=1e+05&limit=10000"

【问题讨论】:

    标签: r httr fastapi


    【解决方案1】:

    我认为自己构建查询字符串最简单,然后您可以在结果上使用URLencode

    url <- "query/Crude/?actual_table_name=live.crude&report_id=xxxxxxx&fields=IMO&where={\"Barrels\":{\"gt\":1},\"conjunction\":\"\"}&where={\"Load Date\":{\"gt\":\"'2000-01-01'\"},\"conjunction\":\"\"}&offset=1e+05&limit=10000"
    
    URLencode(url)
    #> [1] "query/Crude/?actual_table_name=live.crude&report_id=xxxxxxx&fields=IMO&where=%7B%22Barrels%22:%7B%22gt%22:1%7D,%22conjunction%22:%22%22%7D&where=%7B%22Load%20Date%22:%7B%22gt%22:%22'2000-01-01'%22%7D,%22conjunction%22:%22%22%7D&offset=1e+05&limit=10000"
    

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多