红宝石卷曲请求

时间:2012-07-18 00:39:30

标签: ruby http

我如何在ruby中发出以下curl请求?

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'

我对某些标题有困难。

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

1 个答案:

答案 0 :(得分:7)

我认为您需要创建一个请求对象,而不是HTTP对象。然后在其上设置标题。

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

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