gpt4 book ai didi

ruby - ruby 中的 curl 请求

转载 作者:数据小太阳 更新时间:2023-10-29 07:14:34 24 4
gpt4 key购买 nike

我如何在 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'

我在处理某些 header 时遇到困难。

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

最佳答案

我认为您需要创建一个请求对象,而不是一个 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

关于ruby - ruby 中的 curl 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11532792/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com