Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby RestClient GET request with payload

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?

like image 974
Andriy Avatar asked Sep 10 '26 12:09

Andriy


1 Answers

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'}
)
like image 72
garythegoat Avatar answered Sep 13 '26 04:09

garythegoat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!