Introduction

The HttpRequest class is based on the ‘net/http’ and ‘net/ftp’ libraries, so the return type is Net::HTTPResponse or Net::FTP when you call get or post or other methods by HttpRequest.xxx or HttpRequest.ftp

Options

you can call like HttpRequest.get(options), the options parameter is a hash, support following keys:
  :url              =>   String, the url you want to request
  :parameters       =>   String or Hash,  parameters will send to the url
  :redirect         =>   Boolean, whether support redirect to,  default is true
  :redirect_limits  =>   Fixnum, maximal times for redirect if enabled
  :ssl_port         =>   Fixnum, ssl port, default is 443
  :headers          =>   Hash, you can add some custom http headers
  :files            =>   for upload files

  # proxy settings
  :proxy_addr       =>   String, proxy address 
  :proxy_port       =>   Fixnum, proxy port
  :proxy_user       =>   String, proxy username
  :proxy_pass       =>   String, proxy password

Examples for your ruby program:

include http_request.rb first

require '/path/to/http_request.rb'

get

puts HttpRequest.get('http://www.github.com')

get with query string, 4 are same

puts HttpRequest.get('http://www.google.com/search?hl=en&q=ruby&start=0&sa=N')
puts HttpRequest.get(:url => 'http://www.google.com/search', :parameters => 'hl=en&q=ruby&start=0&sa=N')
puts HttpRequest.get({:url => 'http://www.google.com/search', :parameters => 'hl=en&q=ruby&start=0&sa=N'})
puts HttpRequest.get({:url => 'http://www.google.com/search', :parameters => {:hl => 'en', :q => 'ruby', :start => 0, :sa => 'N'}})

post with some paramet

puts HttpRequest.get(:url => 'http://localhost/test.php', :parameters => 'from=http_request.rb').body
puts HttpRequest.get(:url => 'http://localhost/test.php', :parameters => {:name => 'Ruby', :time => 'Now'}).body

also support other http methods, such as put, delete, trace, options, move etc.

HttpRequest.put(:url => 'http://www.example.com', :parameters => 'some=vars')
HttpRequest.delete('http://www.example.com/article/1')
HttpRequest.trace('http://www.example.com/')

basic authorization

HttpRequest.get('http://admin:[email protected]/secret/get/file')

proxy support

HttpRequest.get(:url => 'http://www.example.com/', :proxy_addr => 'your.proxy.address', :proxy_port => 80)
HttpRequest.get(:url => 'http://www.example.com/', :proxy_addr => 'your.proxy.address', :proxy_port => 80, :proxy_user => 'admin', :proxy_pass => '123123')

fetch headers

HttpRequest.get('http://www.example.com/').each {|k, v|
  print "#{k} : #{v}"
}

fetch cookies

hp = HttpRequest.get('http://www.yahoo.com')
hp.cookies.each {|k, v|
   puts "#{k} => #{v}"
}

add cookies into header

HttpRequest.get(:url => 'http://www.example.com/', :cookies => {:login => 'Yes', :userid => 101})
HttpRequest.get(:url => 'http://www.example.com/', :cookies => 'login=Yes; userId=101')

upload file by post method

HttpRequest.post(
  :url => 'http://localhost/upload.php', 
  :files => [{
    :file_name     => 'test.txt',            # original file name, default is rand name such as 0cdex_0
    :field_name    => 'user_file',           # input field name, default is "files[]"
    :content_type  => 'text/plain',          # content type, default is application/octet-stream
    :file_content  => 'Have a nice day!'     # file content
  }]
)

upload more than 1 file

files = [
    {:file_name => 'a.txt', :file_content => 'just for test'},
    {:file_name => 'b.csv', :file_content => "a,b,c\nd,e,f"}
]
HttpRequest.post(
  :url => 'http://localhost/upload.php',
  :files => files
)

upload files with parameters

HttpRequest.post(
  :url => 'http://localhost/upload.php',
  :parameters => {:name => 'zhou', :age => '?'},
  :files => [{:file_content => 'so easy:-)'}]
)
HttpRequest.post(
  :url => 'http://localhost/upload.php',
  :parameters => 'target=php&client=ruby',
  :files => [{:file_content => 'so easy:-)'}]
)

want to upload a binary file such as photo?

HttpRequest.post(
  :url => 'http://localhost/upload.php',
  :parameters => {:title => 'Nice photo', :description => 'some description here.'},
  :files => [{:file_name => 'nice.jpg', :field_name => 'photo', :file_content => File.read('/path/to/nice.jpg')}]
)

upload file by put method, more can check www.php.net/manual/en/features.file-upload.put-method.php

HttpRequest.put(
  :url        => 'http://localhost/upload.php',
  :parameters => 'file content here'
)

Examples in command line:

You need to do like “chmod +x http_request.rb” first. Usage: ./http_request.rb method url [parameters]

get a file and print the content

$./http_request.rb get http://feeds.feedburner.com/RidingRails
$./http_request.rb get 'http://www.google.com/search?hl=en&q=ruby&start=0&sa=N'

get but just print header

$./http_request.rb get_only_header http://feeds.feedburner.com/RidingRails

get header and content

$./http_request.rb get_with_header http://feeds.feedburner.com/RidingRails

download and save as a file

$./http_request.rb http://rubyforge.org/frs/download.php/51094/RMagick-2.9.1.tar.bz2 > rmagick.tar.bz2

post

$./http_request.rb post http://localhost/test.php 'name=Ruby&time=Now'

such as “get_only_header” and “get_with_header”, post and other http methods also can do such as “post_only_header”, “put_with_header” etc.

Examples for FTP (since 1.0.1):

download and save to

ftp = HttpRequest.ftp(:get, :url => 'ftp://user:[email protected]/path/to/hello.mp3', :to => '/tmp/hello.mp3')

upload from local

ftp = HttpRequest.ftp(:put, :url => 'ftp://user:[email protected]/path/to/hello.mp3', :from => '/tmp/hello.mp3')

get server info

puts HttpRequest.ftp(:status, :url => 'ftp://user:[email protected]/')

create a new directory (only for last directory)

HttpRequest.ftp(:mkdir, :url => 'ftp://user:[email protected]/path/to/newdir')
HttpRequest.ftp(:mkdir, :url => 'ftp://user:[email protected]/newdir')

remove a directory (only for last directory)

HttpRequest.ftp(:mkdir, :url => 'ftp://user:[email protected]/path/to/willbe_removed_dir')

list files

puts HttpRequest.ftp(:list, :url => 'ftp://user:[email protected]/')

list files as array

HttpRequest.ftp(:nlst, :url => 'ftp://my.domain.name/', :username => 'user', :password => 'pass').each {|f|
  puts f
}

anonymous login

puts HttpRequest.ftp(:status, :url => 'ftp://my.domain.name/')

working as the “net/ftp” style, need set :close to false

ftp = HttpRequest.ftp(:status, :url => 'ftp://user:[email protected]/', :close => false)
puts ftp.response  # get status from the ftp server
ftp.chdir('/musics')
ftp.getbinaryfile('test.ogg', '/tmp/test.ogg')
ftp.close

download multiple files from a directory

ftp = HttpRequest.ftp('nlst', :url => 'ftp://user:[email protected]/mp3/', :close => false)
ftp.response.each {|f|
    puts "downloading....#{f}"
    ftp.get(f, '/tmp/downloads/' + File.basename(f))
}
ftp.close

TODO

......

LATEST VERSION

1.0.1

Author

xianhua.zhou<xianhua.zhou at gmail.com>, homepage: http://my.cnzxh.net