Ruby 带有负载的 RestClient GET 请求

Ruby RestClient GET request with payload

请帮我解决以下问题: 我有很多申请和相关记录。

app with id 1 has: record1, record2 and record3.
app with id 2 has: record1 ... record1000 

要过滤掉应用 1 的记录,我正在使用 curl。我可以使用以下命令获取记录:

curl -i -H "accept: application/json" -H "Content-type: application/json"  -X GET -d '{ "filters": { "app_id":"1" }}' http://[rails_host]/v1/records?token=[api_token]

我怎样才能做同样的事情,但使用 ruby RestClient (gem 'rest-client' '1.7.2')

要获取所有记录,我执行以下操作:

RestClient.get("http://[rails_host]/v1/records?token=[api_token]", { :content_type => 'application/json', :accept => 'application/json'})

问题是没有返回应用程序 ID,所以我无法解析响应并获取应用程序 ID = 1 的记录

关于如何将负载添加到 Restclient.get 以过滤掉记录的任何想法?

您可以在文档 here 中看到,唯一接受有效载荷参数的高级助手是 POST、PATCH 和 PUT。要使用负载发出 GET 请求,您需要使用 RestClient::Request.execute

这看起来像:

RestClient::Request.execute(
  method:  :get, 
  url:     "http://[rails_host]/v1/records?token=[api_token]",
  payload: '{ "filters": { "app_id":"1" } }',
  headers: { content_type: 'application/json', accept: 'application/json'}
)