Making HEAD request in Ruby
I don't think that passing in a string to :start is enough; in the docs it looks like it requires a URI object's host and port for a correct address:
uri = URI('http://example.com/some_path?query=string')
Net::HTTP.start(uri.host, uri.port) do |http|
request = Net::HTTP::Get.new uri
response = http.request request # Net::HTTPResponse object
end
You can try this:
require 'net/http'
url = URI('yoururl.com')
Net::HTTP.start(url.host, url.port){|http|
response = http.head('/file.xml')
puts response
}
One thing I noticed - your puts response
needs to be inside the block! Otherwise, the variable response
is not in scope.
Edit: You can also treat the response as a hash to get the values of the headers:
response.each_value { |value| puts value }
How to make an HTTP head request with headers in ruby?
Try this:
require 'net/http'
url = 'http://...'
myusename = '...'
mykey = '...'
request = Net::HTTP.new(url, 80)
request.request_head('/', 'x-auth-user' => myusername, 'x-auth-key' => my_key)
Making HTTP HEAD request with timeout in Ruby
Try this snippet:
require 'net/http'
Net::HTTP.start('www.some_site.com') do |http|
http.open_timeout = 2
http.read_timeout = 2
req = Net::HTTP::Head.new('/')
http.request(req).each { |k, v| puts "#{k}: #{v}" }
end
Hope this is what you're looking for.
UPDATE
Because there is head
method that looks like
def head(path, initheader = nil)
request(Head.new(path, initheader))
end
You can also use this snippet:
require 'net/http'
Net::HTTP.start('www.rubyinside.com') do |http|
http.open_timeout = 2
http.read_timeout = 2
http.head('/').each { |k, v| puts "#{k}: #{v}" }
end
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
Net::HTTP failing on a head request
If you follow the documentation properly, it works just fine. The library implementation probably has some assumptions on the usage when it determines whether to read the payload.
response = nil
Net::HTTP.start('github.com', :use_ssl => true) do |http|
response = http.head('/rails/rails')
end
response.each { |k, v| [k, v] }
How can I perform a Head request using mechanize in Ruby
Just like get
but it's head
instead:
page = agent.head 'http://www.google.com/'
page.body.length
#=> 0
page.header.keys
#=> ["date", "expires", "cache-control", "content-type", "set-cookie", "p3p", "server", "x-xss-protection", "x-frame-options", "transfer-encoding"]
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
Related Topics
How to Run Rails App in a Completely Isolated Instances of Chrome
Omission of Curly Braces for a Hash in an Array
Pg_Dump: [Archiver (Db)] Query Failed: Error: Permission Denied for Relation Abouts
No Such File or Directory @ Rb_Sysopen for External Url/Rails 6.11/Ruby 3
Uri::Invalidurierror: Bad Uri(Is Not Uri) Testing Rails Controllers
How to Stop Bundler from Adding Ruby Version to Gemfile.Lock
What Does "String Literal in Condition" Mean
Why Do We Need Nginx with Thin on Production Setup
Rubocop, How to Disable/Enable Cops on Blocks of Code
Open-Uri Is Not Redirecing Http to Https
P Method in Ruby Hard to Search For
Ruby 2.4 on MAC Os Mojave:Image Not Found ... Related to Openssl.Bundle
Gem Install Wrong Number of Arguments (Given 4, Expected 1)
Conditional Dependency in Ruby Gemspec