Skip to content

httrvstyphoeus

MIT 2 9 982
712.9 thousand (month) May 06 2012 1.4.7(1 year, 2 months ago)
4,073 15 143 MIT
Oct 06 2009 1.0 million (month) 1.4.1(7 months ago)

The aim of httr is to provide a wrapper for the curl package, customised to the demands of modern web APIs.

Key features:

  • Functions for the most important http verbs: GET(), HEAD(), PATCH(), PUT(), DELETE() and POST().
  • Automatic connection sharing across requests to the same website (by default, curl handles are managed automatically), cookies are maintained across requests, and a up-to-date root-level SSL certificate store is used.
  • Requests return a standard reponse object that captures the http status line, headers and body, along with other useful information.
  • Response content is available with content() as a raw vector (as = "raw"), a character vector (as = "text"), or parsed into an R object (as = "parsed"), currently for html, xml, json, png and jpeg.
  • You can convert http errors into R errors with stop_for_status().
  • Config functions make it easier to modify the request in common ways: set_cookies(), add_headers(), authenticate(), use_proxy(), verbose(), timeout(), content_type(), accept(), progress().
  • Support for OAuth 1.0 and 2.0 with oauth1.0_token() and oauth2.0_token(). The demo directory has eight OAuth demos: four for 1.0 (twitter, vimeo, withings and yahoo) and four for 2.0 (facebook, github, google, linkedin). OAuth credentials are automatically cached within a project.

Typhoeus is a Ruby library that allows you to make parallel HTTP requests, which can greatly speed up the process of making multiple requests to different servers. It is built on top of the C library libcurl, which is known for its high performance and reliability.

One of the main features of Typhoeus is its ability to make parallel requests. This means that it can send multiple requests at the same time, and wait for all of them to finish before returning the results. This can greatly reduce the time it takes to make multiple requests, as it eliminates the need to wait for each request to complete before sending the next one.

In addition to its parallelism feature, Typhoeus also provides a convenient and easy-to-use Ruby interface for making HTTP requests. It supports all of the common HTTP methods (GET, POST, PUT, DELETE, etc.) and allows you to set various request options, such as headers, timeouts, and authentication. It also supports streaming responses, which allows you to process large responses piece by piece, rather than loading the entire response into memory at once.

Typhoeus is also supports HTTP/2 protocol which provides faster load times and reduced network usage. It also supports streaming which is an essential feature for large data transfer.

Typhoeus is well-documented, actively maintained, and has a large and active community of users. It is widely used in the Ruby ecosystem and is a popular choice for building high-performance web scraping and data-gathering applications.

Note that Typhoeus can also be used as an adapter in popular alternative package Faraday.

Highlights


http2asyncuses-curl

Example Use


library(httr)

# GET requests:
resp <- GET("http://httpbin.org/get")
status_code(resp)  # status code
headers(resp)  # headers
str(content(resp))  # body

# POST requests: 
# Form encoded
resp <- POST(url, body = body, encode = "form")
# Multipart encoded
resp <- POST(url, body = body, encode = "multipart")
# JSON encoded
resp <- POST(url, body = body, encode = "json")

# setting cookies:
resp <- GET("http://httpbin.org/cookies", set_cookies("MeWant" = "cookies"))
content(r)$cookies  # get response cookies
# GET request
Typhoeus.get("www.example.com")
# POST request
Typhoeus.post("www.example.com/posts", body: { title: "test post", content: "this is my test"})

# make parallel requests:
# hydra is a request queue manager
hydra = Typhoeus::Hydra.hydra
# create request object
first_request = Typhoeus::Request.new("http://example.com/posts/1")
# add complete callbacks
first_request.on_complete do |response|
  # callbacks can queue new requests
  third_url = response.body
  third_request = Typhoeus::Request.new(third_url)
  hydra.queue third_request
end
second_request = Typhoeus::Request.new("http://example.com/posts/2")
# queue requests:
hydra.queue first_request
hydra.queue second_request

hydra.run # this is a blocking call that returns once all requests are complete

Alternatives / Similar


Was this page helpful?