Ruby-on-rails 向 rails 中的请求添加标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9303147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:50:16  来源:igfitidea点击:

Add headers to a request in rails

ruby-on-railshttpheader

提问by Tyler DeWitt

I'm using Rails 3.2.1 to make an HTTP Post.

我正在使用 Rails 3.2.1 制作 HTTP Post。

I need to add X-FORWARDEDFOR to the header. How do I do that in Rails?

我需要将X-FORWARDEDFOR添加到标题中。我如何在 Rails 中做到这一点?

Code:

代码:

post_data = {
  "username" => tabuser
}

response = Net::HTTP.post_form(URI.parse("http://<my php file>"), post_data)

采纳答案by house9

It can be set on the request object:

它可以在请求对象上设置:

request = Post.new(url)
request.form_data = params
request['X-Forwarded-For'] = '203.0.113.195'
request.start(url.hostname, url.port,
        :use_ssl => url.scheme == 'https' ) {|http|
    http.request(request) }

See these Net::HTTP examples:

请参阅这些 Net::HTTP 示例:

https://github.com/augustl/net-http-cheat-sheet/blob/master/headers.rb

https://github.com/augustl/net-http-cheat-sheet/blob/master/headers.rb

回答by equivalent8

I find this more readable

我觉得这更具可读性

require "net/http"
require "uri"

url = URI.parse("http://www.whatismyip.com/automation/n09230945.asp")

req = Net::HTTP::Get.new(url.path)
req.add_field("X-Forwarded-For", "0.0.0.0")
req.add_field("Accept", "*/*")

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

puts res.body

stolen from http://www.dzone.com/snippets/send-custom-headers-rub

盗自http://www.dzone.com/snippets/send-custom-headers-rub

HOWEVER !!

然而 !!

if you want to send 'Accept' header (Accept: application/json) to Rails application, you cannot do:

如果要将“接受”标头 ( Accept: application/json) 发送到 Rails 应用程序,则不能执行以下操作:

req.add_field("Accept", "application/json")

req.add_field("Accept", "application/json")

but do:

但做:

req['Accept'] = 'application/json'

req['Accept'] = 'application/json'

The reason for this that Rails ignores the Accept header when it contains “,/” or “/,” and returns HTML (which add_fieldadds). This is due to really old browsers sending incorrect "Accept" headers.

这样做的原因是 Rails 在包含“,/”或“/,”时忽略 Accept 标头并返回 HTML(add_field添加)。这是由于非常旧的浏览器发送了不正确的“接受”标头。

回答by tommybernaciak

Both answers are ok, but I would add one important thing. If you are using https you must add line that you use ssl:

两个答案都可以,但我要补充一件重要的事情。如果您使用 https,则必须添加使用 ssl 的行:

url = URI.parse('https://someurl.com')
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
req = Net::HTTP::Get.new(url.request_uri)
req["header_name"] = header
response = http.request(req)

Without this use_ssl you will get 'EOFError (end of file reached)'.

如果没有这个 use_ssl,你会得到“EOFError(到达文件结尾)”。

回答by Tom Rossi

The original question was for an http postwhich is what I was looking for. I'm going to include this solution for others who may be looking:

最初的问题是针对我正在寻找的http 帖子。我将为可能正在寻找的其他人提供此解决方案:

require 'net/http'

uri = URI.parse("http://<my php file>")

header = {'X-Forwarded-For': '0.0.0.0'}

# Create the HTTP objects
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri, header)

# Send the request
response = http.request(request)