

It's not pretty.īut lets forget that unpleasantness for the moment and deal with the first problem you'll encounter in trying to automate a browser conversation, getting by the various authentication mechanisms. But undoubtedly you'll have to parse unstructured text representations of a report or get the data out of an excel spreadsheet or even a PDF. If you're lucky, the data may be structured in a comma separated value (CSV) text file. And then you need to deal with the format of the resulting data. That means you need to automate what would typically be browser conversations with various websites to get the data you need. Instead of nice composable services you usually get web sites targeted at people not machines. For example, if you needed to integrate data from several business partners you could call various SOAP or RESTful services, get structured XML, and then transform the results into something you could use.īut unfortunately that's not what happens in the real world. Each service would communicate in a platform and technology agnostic manner and XML would be the lingua franca. hostname, uri.In a mythical IT universe designed around a service oriented architecture (SOA) you could assemble several loosely coupled, autonomous services into a business solution. stat 'cached_response' req = Net :: HTTP :: Get. See RFC 2616 section 9.3 for further details. If the files has not been modified since the time in the header a Not Modified response will be returned. The following example performs a conditional GET using the If-Modified-Since header. Other requests that can contain a body such as PUT can be created in the same way using the corresponding request class ( Net::HTTP::Put). To send multipart/form-data use Net::HTTPHeader#set_form: req = Net :: HTTP :: Post. request( req)Įnd case res when Net :: HTTPSuccess, Net :: HTTPRedirection # OK else res. This example creates a URL encoded POST body: uri = URI( '') value end end print fetch( '')Ī POST can be made using the Net::HTTP::Post request class. to_hash # => Array puts "Headers: #" fetch( location, limit - 1)Įlse response.

get_fields( 'set-cookie') # => Array res. The request types Net::HTTP supports are listed below in the section “HTTP Request Classes”.įor all the Net::HTTP request objects and shortcut request methods you may supply either a String for the request path or a URI from which Net::HTTP will extract the request path. If you wish to re-use a connection across multiple HTTP requests without automatically closing it you can use ::new and then call start and finish manually. The connection will remain open for multiple requests in the block if the server indicates it supports persistent connections. Net::HTTP::start immediately creates a connection to an HTTP server which is kept open for the duration of the block. request request # Net::HTTPResponse object end port) do | http | request = Net :: HTTP :: Get.
#Ruby http client code#
The following example code can be used as the basis of an HTTP user-agent which can perform a variety of request types using persistent connections. body POST with Multiple Values ¶ ↑ uri = URI( '') get( uri) # => String GET with Dynamic Parameters ¶ ↑ uri = URI( '') get( '', '/index.html') # => String GET by URI ¶ ↑ uri = URI( '') They are not recommended if you are performing many HTTP requests. The Net::HTTP methods in the following section do not persist connections. This will also require 'uri' so you don't need to require it separately. Simple Examples ¶ ↑Īll examples assume you have loaded Net::HTTP with: require 'net/http' If you are only performing a few GET requests you should try OpenURI. URI::HTTP#host, URI::HTTP#port and URI::HTTP#request_uri are designed to work with Net::HTTP.


For more details about HTTP see ( Net::HTTP is designed to work closely with URI. Net::HTTP provides a rich library which can be used to build HTTP user-agents.
