Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

curl request in ruby

Tags:

http

ruby

How would I make the following curl request in ruby?

curl -k -X GET -H "Content-Type: application/xml" -H "Accept: application/xml" -H "X-OFFERSDB-API-KEY: demo" 'http://testapi.offersdb.com/distribution/beta/offers?radius=10&postal_code=30305'

I'm having difficulty with some of the headers.

require 'net/http'

url = 'http://testapi.offersdb.com/distribution/beta/offers?radius=10&postal_code=30305'
mykey = 'demo'

request = Net::HTTP.new(url)
request.request_head('/', 'X-OFFERSDB-API-KEY' => mykey)

puts request
like image 444
locoboy Avatar asked Jul 18 '12 00:07

locoboy


1 Answers

I think you need to create a request object, instead of an HTTP object. Then set headers on it.

require 'net/http'

url = 'http://testapi.offersdb.com/distribution/beta/offers?radius=10&postal_code=30305'
mykey = 'demo'
uri = URI(url)

request = Net::HTTP::Get.new(uri.path)
request['Content-Type'] = 'application/xml'
request['Accept'] = 'application/xml'
request['X-OFFERSDB-API-KEY'] = mykey

response = Net::HTTP.new(uri.host,uri.port) do |http|
  http.request(request)
end

puts response

Source: http://ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTPHeader.html

like image 194
choover Avatar answered Sep 24 '22 17:09

choover