httr: Tools for Working with URLs and HTTP

モダンなAPI操作に強力なパッケージ

> library(httr)

Attaching package: 'httr'
The following objects are masked from 'package:git2r':

    config, content

バージョン: 1.1.0.9000


関数名 概略
BROWSE Open specified url in browser.
DELETE Send a DELETE request.
GET GET a url.
HEAD Get url HEADers.
PATCH Send PATCH request to a server.
POST POST file to a server.
PUT Send PUT request to server.
VERB VERB a url.
add_headers Add additional headers to a request.
authenticate Use http authentication.
brew_dr Diagnose common configuration problems
cache_info Compute caching information for a response.
config Set curl options.
content Extract content from a request.
content_type Set content-type and accept headers.
cookies Access cookies in a response.
handle Create a handle tied to a particular host.
headers Extract the headers from a response
http_error Take action on http error.
http_status Give information on the status of a request.
httr 'httr' makes http easy.
httr_options List available options.
modify_url Modify a url.
oauth1.0_token Generate an oauth1.0 token.
oauth2.0_token Generate an oauth2.0 token.
oauth_app Create an OAuth application.
oauth_endpoint Describe an OAuth endpoint.
oauth_endpoints Popular oauth endpoints.
oauth_service_token Generate OAuth token for service accounts.
parse_http_date Parse and print http dates.
parse_url Parse and build urls according to RFC1808.
progress Add a progress bar.
response The response object.
revoke_all Revoke all OAuth tokens in the cache.
safe_callback Generate a safe R callback.
set_config Set (and reset) global httr configuration.
set_cookies Set cookies.
status_code Extract status code from response.
timeout Set maximum request time.
upload_file Upload a file with 'POST' or 'PUT'.
url_ok Check for an http OK status.
url_success Check for an http success status.
use_proxy Use a proxy to connect to the internet.
user_agent Set user agent.
verbose Give verbose output.
with_config Execute code with configuration set.
write_disk Control where the response body is written.
write_stream Process output in a streaming manner.

BROWSE

特定のURLをブラウザで開く

Arguments

  • url
  • config
  • ...
  • handle
> BROWSE(url = "http://google.com")

GET

指定されたURIに対する内容を取得する

Arguments

  • url
  • config... authenticate(), add_headers(), set_cookies(), config()
  • ...
> GET("https://api.github.com/events") %>% {
+   url <<- .
+   class(.)
+ }
Error in function_list[[1L]](value): cannot change value of locked binding for 'url'

PATCH

POST

Arguments

  • url
  • config
  • body
  • encode
  • multipart
  • handle
> b2 <- "http://httpbin.org/post"
> POST(b2, body = list(x = "A simple text string"), encode = "json")
Response [http://httpbin.org/post]
  Date: 2016-02-27 14:50
  Status: 200
  Content-Type: application/json
  Size: 503 B
No encoding supplied: defaulting to UTF-8.
{
  "args": {}, 
  "data": "{\"x\":\"A simple text string\"}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "application/json, text/xml, application/xml, */*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "28", 
    "Content-Type": "application/json", 
...

PUT

authenticate

HTTP認証(BASIC認証)

> authenticate(user, password, type = "basic")
> 
> GET("http://httpbin.org/basic-auth/user/passwd",
+   authenticate("user", "passwd"))

content

Arguments

  • x
  • as... raw, text or parsed
  • type
  • encoding
> content(url) %>% str(max.level = 2)
Error: is.response(x) is not TRUE

content_type

> GET("http://httpbin.org/headers", content_type("text/csv"))
Response [http://httpbin.org/headers]
  Date: 2016-02-27 14:50
  Status: 200
  Content-Type: application/json
  Size: 256 B
No encoding supplied: defaulting to UTF-8.
{
  "headers": {
    "Accept": "application/json, text/xml, application/xml, */*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Type": "text/csv", 
    "Host": "httpbin.org", 
    "User-Agent": "libcurl/7.43.0 r-curl/0.9.6 httr/1.1.0.9000"
  }
}

headers

レスポンスヘッダーの情報を取得

> headers(url)
Error in UseMethod("headers"): no applicable method for 'headers' applied to an object of class "function"

oauth1.0_token

> my_app <- oauth_app(appname = "twitter", key = Sys.getenv("TWITTER_KEY"), secret = Sys.getenv("TWITTER_SECRET"))
> twitter_token <- oauth1.0_token(oauth_endpoints("twitter"), my_app)
> 
> req <- GET("https://api.twitter.com/1.1/statuses/home_timeline.json",
+   config(token = twitter_token))
> 
> stop_for_status(req)
> content(req)

oauth2.0_token

oauth2.0認証

Arguments

  • endpoint
  • app
  • scope
  • type
  • use_oob
  • as_header
  • cache
> oauth2.0_token(endpoint, app, scope = NULL, type = NULL,
+   use_oob = getOption("httr_oob_default"), as_header = TRUE,
+   cache = getOption("httr_oauth_cache"))

oauth_app

Arguments

  • appname
  • key
  • secret
> my_app <- oauth_app(appname = "twitter", key = Sys.getenv("TWITTER_KEY"), secret = Sys.getenv("TWITTER_SECRET"))

oauth_endpoint

OAuth認証のエンドポイント

Arguments

  • request
  • authorize
  • access
  • ...
  • base_url
> oauth_endpoint(request   = NULL, 
+                authorize = "authorize",
+                access    = "access_token", 
+                base_url  = "https://github.com/login/oauth")
<oauth_endpoint>
 authorize: https://github.com/login/oauth/authorize
 access:    https://github.com/login/oauth/access_token

oauth_endpoints

Arguments

  • name... linkedin, twitter, vimeo, google, facebook, github
> oauth_endpoints("twitter")
<oauth_endpoint>
 request:   https://api.twitter.com/oauth/request_token
 authorize: https://api.twitter.com/oauth/authenticate
 access:    https://api.twitter.com/oauth/access_token

oauth_service_token

Arguments

  • endpoint
  • secrets
  • scope
> oauth_service_token()

status_code

レスポンスに対するステータスコードの表示

> status_code(url)
Error in UseMethod("status_code"): no applicable method for 'status_code' applied to an object of class "function"

upload_file