Equivalent of cURL for Ruby?
Use OpenURI and
open("http://...", :http_basic_authentication=>[user, password])
accessing sites/pages/resources that require HTTP authentication.
Ruby equivalent of this curl command
Using rest-client
RestClient.post 'localhost:9200/_bulk', File.new("filename.json", 'rb'), 'Content-type' => 'application/x-www-form-urlencoded'
not sure if rest-client
automatically set content-type
, just check without it.
Ruby equivalent of curl with -H (header) switch
Here's an example how this can be accomplished with a standard library net/http:
require 'net/http'
url = '...'
uri = URI(url)
http = Net::HTTP.new(uri.host)
request = Net::HTTP::Get.new(uri.path)
request['authorization'] = %{Token token="#{password}", userID="#{user_id}"}
res = http.request(request)
Converting curl to ruby equivalent
I think you just need to serialise the ruby structure to JSON in the second param. The param should be the string to POST, not a Ruby struct.
I think this will work (only other possible problem I can see is whether you'll connect via https as the code is written):
data = {"channels": ['Giants'], "data": {alert: 'un mensaje '}}
puts Parse.post("/1/push", body: data.to_json)
. . . the JSON-like format in the Ruby data structure is not JSON,
foo: "bar"
is just another Ruby (1.9+) way of saying
:foo => "bar"
Related Topics
How to Make a Http Request Using Ruby on Rails
What Does the "Map" Method Do in Ruby
Sort Hash by Key, Return Hash in Ruby
How to Specify a Local Gem in My Gemfile
Pass Variables to Ruby Script Via Command Line
Why Does Ruby Have Both Private and Protected Methods
Using Rails Serialize to Save Hash to Database
How to Determine If One Array Contains All Elements of Another Array
Test If String Is a Number in Ruby on Rails
Naked Asterisk as Parameter in Method Definition: Def F(*)
Git, Heroku: Pre-Receive Hook Declined
Rails Sessions Current Practices
Ruby Style: How to Check Whether a Nested Hash Element Exists
What Are the Brackets [5.1] After Activerecord Migration and How Does It Work