I'm using Rails 3.2.1 to make an HTTP Post.
我正在使用Rails 3.2.1制作HTTP Post。
I need to add X-FORWARDED
FOR to the header. How do I do that in Rails?
我需要在标题中添加X-FORWARDED FOR。我如何在Rails中做到这一点?
Code:
post_data = {
"username" => tabuser
}
response = Net::HTTP.post_form(URI.parse("http://<my php file>"), post_data)
Thanks
4 个解决方案
#1
9
These net::http examples might be helpful?
这些net :: http示例可能有帮助吗?
https://github.com/augustl/net-http-cheat-sheet/blob/master/headers.rb
#2
7
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:
如果你想向Rails应用程序发送'Accept'标题(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_field
adds). This is due to really old browsers sending incorrect "Accept" headers.
原因是当Rails包含“,/”或“/”时忽略Accept标头并返回HTML(add_field添加)。这是因为很老的浏览器发送了不正确的“Accept”标头。
#3
5
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(到达文件末尾)'。
#4
0
The original question was for an http post which 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)
#1
9
These net::http examples might be helpful?
这些net :: http示例可能有帮助吗?
https://github.com/augustl/net-http-cheat-sheet/blob/master/headers.rb
#2
7
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:
如果你想向Rails应用程序发送'Accept'标题(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_field
adds). This is due to really old browsers sending incorrect "Accept" headers.
原因是当Rails包含“,/”或“/”时忽略Accept标头并返回HTML(add_field添加)。这是因为很老的浏览器发送了不正确的“Accept”标头。
#3
5
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(到达文件末尾)'。
#4
0
The original question was for an http post which 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)