curl request in ruby

2019-06-22 15:32发布

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

标签: ruby http
1条回答
闹够了就滚
2楼-- · 2019-06-22 15:47

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

查看更多
登录 后发表回答