request
is DSL for http requests for R, and is inspired by the CLI tool httpie.
request
is built on httr
, though may allow using the R packages RCurl
or curl
as optional backends at some point.
I gave a poster at User2016, its in my talks repo
applications/json
by default, but give back other types if notdplyr
, etc.GET
requests by default. Specify a different type if you don't want GET
&
's, URL escaping, etc. (see api_query()
)api_body()
)All of the defaults just mentioned can be changed.
Auto execute http requests with pipesWhen using pipes, we autodetect that a pipe is being used within the function calls, and automatically do the appropriate http request on the last piped function call. When you call a function without using pipes, you have to use the http()
function explicitly to make the http request.
Low level access is available with http_client()
, which returns an R6
class with various methods for inspecting http request results.
The function peep()
let's you peek at a request without performing the http request.
From CRAN
install.packages("request")
Development version from GitHub
remotes::install_github("sckott/request")
NSE is supported
api('https://api.github.com/') %>% api_path(repos, ropensci, rgbif, issues)
as well as SE
api('https://api.github.com/') %>% api_path_('repos', 'ropensci', 'rgbif', 'issues')
Works with full or partial URLs
api('https://api.github.com/') #> URL: https://api.github.com/ api('http://api.gbif.org/v1') #> URL: http://api.gbif.org/v1 api('api.gbif.org/v1') #> URL: http://api.gbif.org/v1
Works with ports, full or partial
api('http://localhost:9200') #> URL: http://localhost:9200 api('localhost:9200') #> URL: http://localhost:9200 api(':9200') #> URL: http://localhost:9200 api('9200') #> URL: http://localhost:9200 api('9200/stuff') #> URL: http://localhost:9200/stuff
The above examples with api()
are not passed through a pipe, so only define a URL, but don't do an HTTP request. To make an HTTP request, you can either pipe a url or partial url to e.g., api()
, or call http()
at the end of a string of function calls:
'https://api.github.com/' %>% api() #> $current_user_url #> [1] "https://api.github.com/user" #> #> $current_user_authorizations_html_url #> [1] "https://github.com/settings/connections/applications{/client_id}" #> #> $authorizations_url #> [1] "https://api.github.com/authorizations" #> #> $code_search_url ...
Or
api('https://api.github.com/') %>% http() #> $current_user_url #> [1] "https://api.github.com/user" #> #> $current_user_authorizations_html_url #> [1] "https://github.com/settings/connections/applications{/client_id}" #> #> $authorizations_url #> [1] "https://api.github.com/authorizations" #> #> $code_search_url ...
http()
is called at the end of a chain of piped commands, so no need to invoke it. However, you can if you like.
repo_info <- list(username = 'craigcitro', repo = 'r-travis') api('https://api.github.com/') %>% api_template(template = 'repos/{{username}}/{{repo}}/issues', data = repo_info) %>% peep #> <http request> #> url: https://api.github.com/ #> paths: #> query: #> body: #> paging: #> headers: #> rate limit: #> retry (n/delay (s)): / #> error handler: #> write: #> config:
api_path()
adds paths to the base URL (see api_query()
) for query parameters
api('https://api.github.com/') %>% api_path(repos, ropensci, rgbif, issues) %>% peep #> <http request> #> url: https://api.github.com/ #> paths: repos/ropensci/rgbif/issues #> query: #> body: #> paging: #> headers: #> rate limit: #> retry (n/delay (s)): / #> error handler: #> write: #> config:
api("http://api.plos.org/search") %>% api_query(q = ecology, wt = json, fl = 'id,journal') %>% peep #> <http request> #> url: http://api.plos.org/search #> paths: #> query: q=ecology, wt=json, fl=id,journal #> body: #> paging: #> headers: #> rate limit: #> retry (n/delay (s)): / #> error handler: #> write: #> config:
See the issues for discussion of these
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4