Ruby - Send GET request with headers
Using net/http
as suggested by the question.
References:
Net::HTTP
https://ruby-doc.org/stdlib-2.4.1/libdoc/net/http/rdoc/Net/HTTP.htmlNet::HTTP::get
https://ruby-doc.org/stdlib-2.4.1/libdoc/net/http/rdoc/Net/HTTP.html#method-c-get- Setting headers: https://ruby-doc.org/stdlib-2.4.1/libdoc/net/http/rdoc/Net/HTTP.html#class-Net::HTTP-label-Setting+Headers
Net::HTTP::Get
https://ruby-doc.org/stdlib-2.4.1/libdoc/net/http/rdoc/Net/HTTP/Get.htmlNet::HTTPGenericRequest
https://ruby-doc.org/stdlib-2.4.1/libdoc/net/http/rdoc/Net/HTTPGenericRequest.html andNet::HTTPHeader
https://ruby-doc.org/stdlib-2.4.1/libdoc/net/http/rdoc/Net/HTTPHeader.html (for methods that you can call onNet::HTTP::Get
)
So, for example:
require 'net/http'
uri = URI("http://www.ruby-lang.org")
req = Net::HTTP::Get.new(uri)
req['some_header'] = "some_val"
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') { |http|
http.request(req)
}
puts res.body # <!DOCTYPE html> ... </html> => nil
Note: if your res
ponse has HTTP result state 301 (Moved permanently), see Ruby Net::HTTP - following 301 redirects
Ruby NET::HTTP Read the header BEFORE the body (without HEAD request)?
net/http
supports streaming, you can use this to read the header before the body.
Code example,
url = URI('http://stackoverflow.com/questions/41306082/ruby-nethttp-read-the-header-before-the-body-without-head-request')
Net::HTTP.start(url.host, url.port) do |http|
request = Net::HTTP::Get.new(url)
http.request(request) do |response|
# check headers here, body has not yet been read
# then call read_body or just body to read the body
if true
response.read_body do |chunk|
# process body chunks here
end
end
end
end
Show headers and body text of an HTTP request in Ruby
The #to_hash method of a request object may be useful. Here's an example to build a GET request and inspect headers:
require 'net/http'
require 'uri'
uri = URI('http://example.com/cached_response')
req = Net::HTTP::Get.new(uri.request_uri)
req['X-Crazy-Header'] = "This is crazy"
puts req.to_hash # hash of request headers
# => {"accept"=>["*/*"], "user-agent"=>["Ruby"], "x-crazy-header"=>["This is crazy"]}
And an example for a POST request to set form data and inspect headers and body:
require 'net/http'
require 'uri'
uri = URI('http://www.example.com/todo.cgi')
req = Net::HTTP::Post.new(uri.path)
req.set_form_data('from' => '2005-01-01', 'to' => '2005-03-31')
puts req.to_hash # hash of request headers
# => {"accept"=>["*/*"], "user-agent"=>["Ruby"], "content-type"=>["application/x-www-form-urlencoded"]}
puts req.body # string of request body
# => from=2005-01-01&to=2005-03-31
how do I include a header in an http request in ruby
get_response
is a shorthand for making a request, when you need more control - do a full request yourself.
There's an example in ruby standard library here:
uri = URI.parse("http://www.ncdc.noaa.gov/cdo-web/api/v2/datasets/")
req = Net::HTTP::Get.new(uri)
req['token'] = 'fjhKJFSDHKJHjfgsdfdsljh'
res = Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req)
}
Get response headers from Ruby HTTP request
The response object actually contains the headers.
See "Net::HTTPResponse" for more infomation.
You can do:
response['Cache-Control']
You can also call each_header
or each
on the response object to iterate through the headers.
If you really want the headers outside of the response object, call response.to_hash
How to make an HTTP GET with modified headers?
Created a solution that worked for me (worked very well) - this example getting a range offset:
require 'uri'
require 'net/http'
size = 1000 #the last offset (for the range header)
uri = URI("http://localhost:80/index.html")
http = Net::HTTP.new(uri.host, uri.port)
headers = {
'Range' => "bytes=#{size}-"
}
path = uri.path.empty? ? "/" : uri.path
#test to ensure that the request will be valid - first get the head
code = http.head(path, headers).code.to_i
if (code >= 200 && code < 300) then
#the data is available...
http.get(uri.path, headers) do |chunk|
#provided the data is good, print it...
print chunk unless chunk =~ />416.+Range/
end
end
Related Topics
Using SASS with User-Specified Colors
How to Set a Variable from a Helper Method for Inclusion in a SASS SCSS Stylesheet
Using Phonegap as a Native Container for a Rails 3 App
Ruby on Rails - Doesn't Create Script/Server
Nokogiri: Searching for <Div> Using Xpath
Consequences of Implementing To_Int and To_Str in Ruby
Rspec: Should Be (This or That)
What Is Difference Between "P" and "Pp"
How to Store an Instance Variable Across Multiple Actions in a Controller
Slicing Params Hash for Specific Values
Changing Songs on Jplayer by Clicking a Link, Hosted on Amazon S3