3

Please help me with the following: I have a number of application and associated records.

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

To filter out records for app 1 Im using curl. I can get records with the following command:

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]

How can I do the same but with ruby RestClient (gem 'rest-client' '1.7.2')

To get all records I do the following:

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

The Problem is that app ids are not returned so I cannot parse response and take records that has app id = 1

Any ideas how can I add payload to Restclient.get to filter out records?

Andriy
  • 94
  • 1
  • 9
  • Using a body with a GET is a bad design. Technically you can do this but to meet the spec the body of the GET should in no way influence the response (so the same response should come back regardless of the body). Furthermore, many people (and thus, implementations) assume GET requests cannot have a body that matters, so many implementations will not allow you to send a body. Basically: don't build an API where GET requires/uses data in the request body. – Joe Sep 23 '17 at 21:13
  • Unfortunately, Im not building an application. Currently, Im using existing one and trying to create continuous integration solution using existing functionality. Thanks – Andriy Sep 24 '17 at 09:51

1 Answers1

9

You can see in the docs here that the only high level helpers that accept a payload argument are POST, PATCH, and PUT. To make a GET request using a payload you will need to use RestClient::Request.execute

This would look like:

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'}
)
garythegoat
  • 1,517
  • 1
  • 12
  • 25