使用哈希数组调用API调用

时间:2018-06-11 15:30:03

标签: json rest curl

我们使用名为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的样本中的任何字段。 E.g:

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值= 111222333应返回一些内容,但它只是给出一个空白结果:

enter image description here enter image description here

示例命令:

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解释/传递网址的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}

2 个答案:

答案 0 :(得分:1)

您必须对网址参数的值进行网址编码。 e.g。

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状态。

答案 1 :(得分:1)

我找到了一个使用--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 .