如何在Ruby下使用修改后的标头创建HTTP请求?

时间:2017-04-18 13:38:43

标签: ruby http http-headers

如何使用modiefied标头在Ruby下创建HTTP请求?

我想添加“PublicKey”和“Accept”等属性。

我想添加一个新的身份验证标头值,如“Basic”。

我的方法:

cat_nickname

1 个答案:

答案 0 :(得分:0)

而不是如此声明请求:Net::HTTP.get(uri), 使用.new

因此,重写代码将是:

uri = URI.parse('https://www.blablubbxxx0.com/')
req = Net::HTTP::Get.new(uri.path)
req.add_field("HeaderName", "HeaderValue")    # Adding a header
req['If-Modified-Since'] = file.mtime.rfc2822 # Setting a header


res = Net::HTTP.new(url.host, url.port).start do |http|
  http.request(req)
end

来自https://dzone.com/articles/send-custom-headers-ruby的代码