【问题标题】:curl API calls with array of hashes带有哈希数组的 curl API 调用
【发布时间】:2018-11-20 21:56:17
【问题描述】:

我们正在使用一个名为 FreezerPro (https://www.freezerpro.com/product-tour) 的服务器软件产品,其 API 可以以编程方式调用。有像 freezers 这样的简单方法可以处理 curl 调用,如下所示:

freezers -- Retrive a list of freezers
Returned objects: Freezers
Required parameters: None
Optional query parameters: None
Optional control parameters: None

curl -g --insecure 'https://username:password@demo-usa.freezerpro.com/api?method=freezers' | jq . | head -n 12
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  8697    0  8697    0     0  15980      0 --:--:-- --:--:-- --:--:-- 15987
{
  "Freezers": [
    {
      "rfid_tag": "355AB1CBC00000700000075A",
      "barcode_tag": "7000001882",
      "boxes": 0,
      "subdivisions": 1,
      "access": 0,
      "description": "[1000000000]",
      "name": "[1000000000]",
      "id": 1882
    },

然后有一个search_samples 方法可以搜索给定query 的样本中的任何字段。例如:

search_samples -- search for samples:
Returned objects: Samples
Required parameters: None
Optional query parameters:
query = <filter text> optional search string to filter the results.
Optional control parameters:
start = <staring record>
limit = <limit number of records to retrieve>
sort = <sort_field>
dir = <ASC / DESC>

curl -g --insecure 'https://username:password@demo-usa.freezerpro.com/api?method=search_samples&query=111222333' | jq .                                                                      
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   347    0   347    0     0    977      0 --:--:-- --:--:-- --:--:--   977
{
  "Samples": [
    {
      "created_at": "06/11/2018",
      "owner_id": 45,
      "owner": "<span ext:qtip=\"username\">username</span>",
      "description": "test",
      "sample_id": 53087,
      "id": 53087,
      "loc_id": 54018,
      "type": "cfDNA",
      "scount": 1,
      "name": "123456AB",
      "location": "ER111&rarr;Level 1&rarr;Level 2&rarr;test001 (1)",
      "icon": "images/box40/i53.png"
    }
  ],
  "Total": 1
}

到目前为止一切顺利。尝试运行advanced_search 查询时会出现问题,该查询在查询部分采用哈希数组。鉴于上面的示例,它有一个名为 patient_id 的 udf 值为 111222333,advanced_search 对 udf patient_id value=111222333 的查询应该返回一些内容,但它只是给出一个空白结果:

示例命令:

curl -g --insecure 'https://username:password@demo-usa.freezerpro.com/api?method=advanced_search&subject_type=Sample&query=[{type="udf",field="patient_id",value=111222333}]'

我正在使用:

curl --version
curl 7.35.0 (x86_64-pc-linux-gnu) libcurl/7.35.0 OpenSSL/1.0.1f zlib/1.2.8 libidn/1.28 librtmp/2.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smtp smtps telnet tftp 
Features: AsynchDNS GSS-Negotiate IDN IPv6 Largefile NTLM NTLM_WB SSL libz TLS-SRP 

这是否与 curl 解释/传递 URL 的 query 部分的方式有关?

关于如何构建查询的任何想法?这是curl 的具体问题吗?

编辑:尝试 curl urlencode,它抱怨没有设置查询:

curl -g -G --insecure  'https://username:password@demo-usa.freezerpro.com/api' --data-urlencode 'method=advanced_search' --data-urlencode 'query=[{type="udf",field="patient_id",value=111222333}]'
{"error":true,"message":"Query or search conditions must be specified","success":false}

【问题讨论】:

    标签: json rest curl


    【解决方案1】:

    您必须对 URL 参数的值进行 URL 编码。例如

    curl -g --insecure 'https://username:password@demo-usa.freezerpro.com/api?method=advanced_search&subject_type=Sample&query=%5B%7Btype%3D%22udf%22%2Cfield%3D%22patient_id%22%2Cvalue%3D111222333%7D%5D'
    

    另外,请使用-v 参数运行 curl 以使其详细,这样我们至少可以知道返回的 HTTP 状态。

    【讨论】:

    • 试过你的命令,抱怨查询不正确。尝试使用 urlencode (现在编辑了答案),同样的问题。还有其他选择吗?
    • 感谢您的回答。通过对 curl 选项的更多修改,我找到了一个现在可以使用的解决方案。感谢您指出 curl 格式的问题。
    【解决方案2】:

    我找到了使用--data 标志和-k 标志的解决方案:

    curl -k --header "Content-Type: application/json" --request GET --data '{"username":"user", "password":"password", "method":"advanced_search", "query":[{"type":"udf","field":"patient_id","op":"eq","value":"111222333"}], "udfs":["patient_id","other"]}' https://demo-usa.freezerpro.com/api | jq .
    

    【讨论】:

      猜你喜欢
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多