Make a HTTP header request with Curb
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.
Using Ruby Curb gem with API token
Here are the 3 parts to my header with my traditional request
- My-Security-Token
- 1234567890
- website.com
Headers consist of name/value pairs, so your description doesn't make any sense.
It looks your header name should be:
My-Security-Token
and it's value should be:
1234567890
To make a GET request
(you didn't mention what type of request you are making) and specify that header, you can do this:
require 'curb'
http = Curl.get("http://website.com/") do |http|
http.headers["My-Security-Token"] = "1234567890"
end
puts http.body_str[0..249] #Output the first 250 characters of the response
If you want to use verbose mode, which will display the actual request and response, you can do this:
require 'curb'
c = Curl::Easy.new("http://www.google.com/") do |curl|
curl.headers["My-Security-Token"] = "1234567890"
curl.verbose = true
end
c.perform #Outputs the request and the response
puts c.body_str[0..249] #body_str => the body of the response
output:
* About to connect() to www.google.com port 80 (#0)
* Trying 74.125.28.147... * Connected to www.google.com (74.125.28.147) port 80 (#0)
> GET / HTTP/1.1
Host: www.google.com
Accept: */*
My-Security-Token: 1234567890 #***HERE IS YOUR CUSTOM HEADER****
< HTTP/1.1 200 OK
< Date: Tue, 03 Mar 2015 00:14:42 GMT
< Expires: -1
< Cache-Control: private, max-age=0
< Content-Type: text/html; charset=ISO-8859-1
< Set-Cookie: PREF=ID=479a8fa626097193:FF=0:TM=1425341682:LM=1425341682:S=5kflGPOAzEx-uMWb; expires=Thu, 02-Mar-2017 00:14:42 GMT; path=/; domain=.google.com
< Set-Cookie: NID=67=Eim3D__PFGbpSWdcIH9IPhkuDEVMFjN4ShU9gA6Z_rMryMoI6nv--sIjk_E00_EpMfSe3RkPO5dYjV7yGTXT3oMLX-t7FsrKSJmF7-OffAuLKrr5KfV1IZzL9yaJQKiB; expires=Wed, 02-Sep-2015 00:14:42 GMT; path=/; domain=.google.com; HttpOnly
< P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
< Server: gws
< X-XSS-Protection: 1; mode=block
< X-Frame-Options: SAMEORIGIN
< Alternate-Protocol: 80:quic,p=0.08
< Accept-Ranges: none
< Vary: Accept-Encoding
< Transfer-Encoding: chunked
<
* Expire cleared
* Connection #0 to host www.google.com left intact
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><head><meta content="Search the world's information, including webpages, images, videos and more. Google has many specia
If you actually want the custom headers to be:
Name: My-Security-Token
Value: 1234567890
Comment: website.com
...then you can do this:
require 'curb'
c = Curl::Easy.new("http://www.google.com/") do |curl|
curl.headers["Name"] = "My-Security-Token"
curl.headers["Value"] = "1234567890"
curl.headers["Comment"] = "website.com"
curl.verbose = true
end
c.perform
puts c.body_str[0..249]
...which will produce output like this:
* About to connect() to www.google.com port 80 (#0)
* Trying 74.125.28.105... * Connected to www.google.com (74.125.28.105) port 80 (#0)
> GET / HTTP/1.1
Host: www.google.com
Accept: */*
Name: My-Security-Token #HERE ARE YOUR HEADERS
Value: 1234567890 #HERE
Comment: website.com #HERE
...
...
If nothing above does what you want, then instead of describing your request, how about posting your actual "traditional" request? If you don't know how to do that, search google.
Curb doesn't respond
It can be related to a network issue happening randomly.
If you use a timeout you can skip this kind of situations in long running tasks.
require 'timeout'
begin
Timeout.timeout(5) do
easy.perform
end
rescue Timeout::Error
puts 'timeout'
end
getting the status code of a HTTP redirected page
Found it.
I cloned the curb source and grepped for :
last_effective_url
In the function below it was the equivalent for the response code, in curb_easy.c, line 2435.
Note to self, "Use the source Luke"!
UPDATE:
The answer is response_code
In my case the code looks like so:
c = Curl::Easy.new(HOST_NAME) do |curl|
curl.follow_location = true
curl.head = true
end
c.perform
puts url + " => " + c.response_code.to_s
Related Topics
Script to Run Against Stdin If No Arg; Otherwise Input File =Argv[0]
Scraping/Parsing Google Search Results in Ruby
Sidekiq Worker Not Getting Triggered
In Ruby or Rails, Why Is "Include" Sometimes Inside the Class and Sometimes Outside the Class
Ruby on Rails Map.Root Doesn't Seem to Be Working
Rvm System-Wide Install Script Url Broken -- What Is Replacement
Running into Issues with Rvm During Ruby Install (1.9.2)
No Implicit Conversion from Nil to Integer - When Trying to Add Anything to Array
How to Shorten a Uuid to a Specific Length
How to Extend Redcarpet to Support Auto Linking User Mentions
Using Activemodel::Serializer in Rails - JSON Data Differs Between JSON and Index Response
How to Fix a Slow Implicit Query on Pg_Attribute Table in Rails
Get Response Headers from Curb
How to Set Environment Variable Using Chef
Ruby: Differencebetween the Comparatives: "||" and "Or"
Displaying a Polygon with Gmaps4Rails
Problems Keeping an Object in an Array, Ruby Issues and Rails Issues