Question

With curl I can perform a HTTP header request like so:

curl -I 'http://www.google.com'

How can I perform this procedure with Curb? I don't want to retrieve the body as this would take too much time.

Was it helpful?

Solution

The -I/--head option performs a HEAD request. With libcurl C API you need to set the CURLOPT_NOBODY option.

With curb, you can set this option on your handle as follow:

h = Curl::Easy.new("http://www.google.com")
h.set :nobody, true
h.perform
puts h.header_str
# HTTP/1.1 302 Found
# Location: http://www.google.fr/
# Cache-Control: private
# Content-Type: text/html; charset=UTF-8
# ...

As an alternative, you can use one of the convenient shortcut like:

h = Curl::Easy.new("http://www.google.com")
# This sets the option behind the scenes, and call `perform`
h.http_head
puts h.header_str
# ...

Or like this one, using the class method:

h = Curl::Easy.http_head("http://www.google.com")
puts h.header_str
# ...

Note: the ultimate shortcut is Curl.head("http://www.google.com"). That being said wait until the next curb release before using it, since it's NOT working at the time of writing, and has just been patched: see this pull request.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top