Skip to content

Instantly share code, notes, and snippets.

@aliang
Created March 9, 2010 17:25
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save aliang/326846 to your computer and use it in GitHub Desktop.
Save aliang/326846 to your computer and use it in GitHub Desktop.
setting custom headers with ruby net/http
# this was surprisingly difficult to find, the documentation and API is poor
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")
# For content type, you could also use content_type=(type, params={})
# req.set_form_data({'query' => 'search me'})
# req['X-Forwarded-For'] = "0.0.0.0"
res = Net::HTTP.new(url.host, url.port).start do |http|
http.request(req)
end
puts res.body
@achempion
Copy link

Net::HTTP is terrible :)

@nozpheratu
Copy link

the documentation and API is poor

Indeed...

@xiconet
Copy link

xiconet commented May 1, 2014

What about https?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment