API POST with array using HTTP gem (or RestClient)
def self.submitorder
itemsarray = { :items => [ { :product_id => 423, :brand_id => 33, :color_id => 498, :size_id => 4, :quantity => 1, :front_print => 1389517,
:front_mockup => 1390617 } ] }
req = HTTP.post("https://api.printaura.com/api.php", :json => {
:key => APIKEY,
:hash => APIHASH,
:method => "addorder",
:businessname => "this is a secret too",
:businesscontact => "thats a secret",
:email => "my@email.com",
:your_order_id => "1",
:returnlabel => "FakeAddress",
:clientname => "ShippingName",
:address1 => "ShippingAddressLine1",
:address2 => "ShippingAddressLine2",
:city => "ShippingCity",
:state => "ShippingState",
:zip => "ShippingZip",
:country => "US",
:customerphone => "dontcallme",
:shipping_id => "1",
:items => Base64.encode64(itemsarray.to_json)}
)
puts JSON.parse(req)
I really hopes this helps somebody some years from now haha
Sending an array of string in a request made with rest-client gem
Posted for visibility (previously answered in comment)
RestClient
will appropriately serialize a native ruby Array
for you meaning
events: ["invitee.#{type}"]
will be serialized into events[]=invitee.created
where type == 'created'
. This will work for an Array
with multiple values too.
events: ["more","than","one"]
will be converted to events[]=more&events[]=than&events[]=one
.
Finally it also handles an other data types inside an Array
such as another Hash
events: [{first_name: 'Zaphod', last_name: 'Beeblebrox'}]
will become events[][first_name]=Zaphod&events[][last_name]=Beeblebrox
RestClient post request in ruby on rails
The difference between the CURL
version and the RestClient
version is that in the CURL
version you send a JSON
string as payload but in the RestClient
sends the string '@user'
.
It should be fine when you actually send JSON:
RestClient.post(
"http://localhost:4123/api/v1/users?token=<token>",
@user.to_json,
{ content_type: :json, accept: :json }
)
rest-client in ruby on rails -multiple request
The error is coming from this line, rather than url[i]
, use just i
, as i is the url you are trying to access, as an example, to deal with the implicit conversion:
render json: JSON.parse(RestClient::Request.execute( method: :get, url: i)), layout: nil
But the core issue is rendering multiple times, this can be solved by getting the data you need first, and then render the array once at the end.
Like so:
def index
url_list = ['http://example1','https://example2']
responses = []
url_list.each do |url|
responses << JSON.parse(RestClient::Request.execute( method: :get, url: url))
end
render json: responses, layout: nil
end
Ruby RestClient GET request with payload
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'}
)
passing json data in elasticsearch get request using rest-client ruby gem
RestClient can't send request bodies with GET
. You've got two options:
Pass your query as the source
URL parameter:
require 'rest_client'
require 'json'
# RestClient.log=STDOUT # Optionally turn on logging
q = '{
"query" : { "term" : { "user" : "kimchy" } }
}
'
r = JSON.parse \
RestClient.get( 'http://localhost:9200/twitter/tweet/_search',
params: { source: q } )
puts r
...or just use POST
.
UPDATE: Fixed incorrect passing of the URL parameter, notice the params
Hash.
Related Topics
Encrypt Data Bag from Inside of Ruby Without Relying on Knife
Why Use Gems for Serving Assets Instead of the Vendor File
Rails Migration Complains About Undefined Method 'Attachment' Using Paperclip
Invalid Source Reflection MACro :Has_Many :Through
Hook Before All Delyed Job Success Callback to Save Successfully Completed Jobs
How to Return a Group of Sequential Numbers That Might Exist in an Array
Managing Conflicting Versions of Ruby Gems
How to Ignore Irrelevant Methods When Profiling Ruby Applications
Instagram Ruby Gem - Unable to Reach Callback Url
Enter & Ioerror: Byte Oriented Read for Character Buffered Io
Connection Refused Using Sunspot and Solr in Rails
Rails 4 Use Application Helpers Inside Initializers
How to Capture Terminal Arrow Keys in Ruby
Rails 3 + Daemons Gem: Exception When Querying Model
Ruby Instance Method & Conditional Local Variable Assignment with Same Name
Numeric Literals Prepended with '0'