How to specify "http request header" in OpenURI
Asked Answered
A

1

42

I am trying to call a URL using Ruby's OpenURI gem, however it needs me to pass certain values inside its HTTP request header.

Any idea how to do this?

Adalard answered 20/9, 2011 at 0:28 Comment(1)
this really helps with 422 error which is caused by Accept parameter expecting to know format like xmlGalatea
D
63

According to the documentation, you can pass a hash of http headers as the second argument to open:

open("http://www.ruby-lang.org/en/",
   "User-Agent" => "Ruby/#{RUBY_VERSION}",
   "From" => "[email protected]",
   "Referer" => "http://www.ruby-lang.org/") {|f|
   # ...
 }
Dorking answered 20/9, 2011 at 1:2 Comment(2)
Use URI.open to avoid deprecations warnings from ruby related to confusion with kernel#open.Gawky
also you can pass headers as a hash, not only double splatted.Incursion

© 2022 - 2024 — McMap. All rights reserved.