17

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

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

Code:

post_data = {
  "username" => tabuser
}

response = Net::HTTP.post_form(URI.parse("http://<my php file>"), post_data)
3
  • These net::http examples might be helpful? github.com/augustl/net-http-cheat-sheet/blob/master/headers.rb Commented Feb 16, 2012 at 6:08
  • Thanks! If you want to put that as an answer I'll accept it Commented Feb 16, 2012 at 17:01
  • added comment as the answer - Cheers! Commented Feb 19, 2012 at 2:28

4 Answers 4

15

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

HOWEVER !!

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

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

but do:

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.

Sign up to request clarification or add additional context in comments.

Comments

10

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:

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

2 Comments

Link-only answers are discouraged. You may want to have actual code on how to add headers within the body of the answer.
@AndrewGrimm - my answer is from over 5 years ago. While not perfect it is what it is.
10

Both answers are ok, but I would add one important thing. If you are using https you must add line that you use 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)'.

1 Comment

This is critical if ssl is required because you can otherwise build a perfect request and still get a '400 Bad Request' response.
1

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:

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.