Equivalent of Curl For Ruby

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



Leave a reply



Submit