Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify "http request header" in OpenURI

Tags:

ruby

open-uri

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?

like image 533
iwan Avatar asked Sep 20 '11 00:09

iwan


People also ask

How do I add a header to my HTTP request?

To add custom headers to an HTTP request object, use the AddHeader() method. You can use this method multiple times to add multiple headers. For example: oRequest = RequestBuilder:Build('GET', oURI) :AddHeader('MyCustomHeaderName','MyCustomHeaderValue') :AddHeader('MySecondHeader','MySecondHeaderValue') :Request.

Do HTTP requests have headers?

A request header is an HTTP header that can be used in an HTTP request to provide information about the request context, so that the server can tailor the response. For example, the Accept-* headers indicate the allowed and preferred formats of the response.

What is header in HTTP request example?

An HTTP header is a field of an HTTP request or response that passes additional context and metadata about the request or response. For example, a request message can use headers to indicate it's preferred media formats, while a response can use header to indicate the media format of the returned body.

Is request URL a header?

Yes, "Location: redirect_url" is an header which tells your browser to make a NEW REQUEST to that url.


1 Answers

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|    # ...  } 
like image 135
cam Avatar answered Sep 22 '22 06:09

cam