GithubHelp home page GithubHelp logo

httpi's Introduction

HTTPI Build Status

HTTPI provides a common interface for Ruby HTTP libraries.

Bugs | RDoc

Installation

HTTPI is available through Rubygems and can be installed via:

$ gem install httpi

Getting started

In order to provide a common interface, HTTPI provides the HTTPI::Request object for you to configure your request. Here's a very simple GET request.

request = HTTPI::Request.new("http://example.com")
HTTPI.get request

To execute a POST request, you may want to specify a payload.

request = HTTPI::Request.new
request.url = "http://post.example.com"
request.body = "some data"

HTTPI.post request

HTTPI

The HTTPI module uses one of the available adapters to execute HTTP requests.

GET

HTTPI.get(request, adapter = nil)
HTTPI.get(url, adapter = nil)

POST

HTTPI.post(request, adapter = nil)
HTTPI.post(url, body, adapter = nil)

HEAD

HTTPI.head(request, adapter = nil)
HTTPI.head(url, adapter = nil)

PUT

HTTPI.put(request, adapter = nil)
HTTPI.put(url, body, adapter = nil)

DELETE

HTTPI.delete(request, adapter = nil)
HTTPI.delete(url, adapter = nil)

Notice

  • You can specify the adapter to use per request
  • And request methods always return an HTTPI::Response

More control

If you need more control over the request, you can access the HTTP client instance represented by your adapter in a block:

HTTPI.post request do |http|
  http.use_ssl = true  # Curb example
end

HTTPI::Adapter

HTTPI uses adapters to support multiple HTTP libraries. It currently contains adapters for:

You can manually specify the adapter to use via:

HTTPI.adapter = :curb  # or one of [:httpclient, :net_http]

If you don't specify which adapter to use, HTTPI try to load HTTPClient, then Curb and finally NetHTTP.

Notice

HTTPI does not force you to install any of these libraries. If you'd like to use on of the more advanced libraries (HTTPClient or Curb), you have to make sure they're in your LOAD_PATH. HTTPI will then load the library when executing HTTP requests.

HTTPI::Request

URL

request.url = "http://example.com"
request.url # => #<URI::HTTP:0x101c1ab18 URL:http://example.com>

Proxy

request.proxy = "http://example.com"
request.proxy # => #<URI::HTTP:0x101c1ab18 URL:http://example.com>

Headers

request.headers["Accept-Charset"] = "utf-8"
request.headers = { "Accept-Charset" => "utf-8" }
request.headers # => { "Accept-Charset" => "utf-8" }

Body

request.body = "some data"
request.body # => "some data"

Open timeout

request.open_timeout = 30 # sec

Read timeout

request.read_timeout = 30 # sec

HTTPI::Auth

HTTPI::Auth supports HTTP basic and digest authentication.

request.auth.basic("username", "password")   # HTTP basic auth credentials
request.auth.digest("username", "password")  # HTTP digest auth credentials

For experimental NTLM authentication, please use the httpi-ntlm gem and provide feedback.

request.auth.ntlm("username", "password")    # NTLM auth credentials

HTTPI::Auth::SSL

HTTPI::Auth::SSL manages SSL client authentication.

request.auth.ssl.cert_key_file     = "client_key.pem"   # the private key file to use
request.auth.ssl.cert_key_password = "C3rtP@ssw0rd"     # the key file's password
request.auth.ssl.cert_file         = "client_cert.pem"  # the certificate file to use
request.auth.ssl.ca_cert_file      = "ca_cert.pem"      # the ca certificate file to use
request.auth.ssl.verify_mode       = :none              # or one of [:peer, :fail_if_no_peer_cert, :client_once]

HTTPI::Response

Every request returns an HTTPI::Response. It contains the response code, headers and body.

response = HTTPI.get request

response.code     # => 200
response.headers  # => { "Content-Encoding" => "gzip" }
response.body     # => "<!DOCTYPE HTML PUBLIC ...>"

The response.body handles gzipped and DIME encoded responses.

TODO

  • Return the original HTTPI::Request for debugging purposes
  • Return the time it took to execute the request

Logging

HTTPI by default logs each HTTP request to STDOUT using a log level of :debug.

HTTPI.log       = false     # disable logging
HTTPI.logger    = MyLogger  # change the logger
HTTPI.log_level = :info     # change the log level

httpi's People

Contributors

benoist avatar fagiani avatar hakanensari avatar magec avatar matthall avatar rubiii avatar vangberg avatar watson avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.