| layout | language | permalink | command | io | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
api-command |
JavaScript |
api/javascript/http/ |
http |
|
{% apibody %} r.http(url[, options]) → value r.http(url[, options]) → stream {% endapibody %}
Retrieve data from the specified URL over HTTP. The return type depends on the resultFormat option, which checks the Content-Type of the response by default. Make sure that you never use this command for user provided URLs.
Example: Perform an HTTP GET and store the result in a table.
r.table('posts').insert(r.http('http://httpbin.org/get')).run(conn, callback)See the tutorial on r.http for more examples on how to use this command.
timeout: timeout period in seconds to wait before aborting the connect (default30).attempts: number of retry attempts to make after failed connections (default5).redirects: number of redirect and location headers to follow (default1).verify: iftrue, verify the server's SSL certificate (defaulttrue).resultFormat: string specifying the format to return results in. One of the following:text: always return a string.json: parse the result as JSON, raising an error on failure.jsonp: parse the result as Padded JSON.binary: return a binary object.auto: parse the result based on itsContent-Type(the default):application/json: asjsonapplication/json-p,text/json-p,text/javascript: asjsonpaudio/*,video/*,image/*,application/octet-stream: asbinary- anything else: as
text
method: HTTP method to use for the request. One ofGET,POST,PUT,PATCH,DELETEorHEAD. Default:GET.auth: object giving authentication, with the following fields:type:basic(default) ordigestuser: usernamepass: password in plain text
params: object specifying URL parameters to append to the URL as encoded key/value pairs.{ query: 'banana', limit: 2 }will be appended as?query=banana&limit=2. Default: no parameters.header: Extra header lines to include. The value may be an array of strings or an object. Default:Accept-Encoding: deflate;q=1, gzip;q=0.5andUser-Agent: RethinkDB/<VERSION>.data: Data to send to the server on aPOST,PUT,PATCH, orDELETErequest. ForPOSTrequests, data may be either an object (which will be written to the body as form-encoded key/value pairs) or a string; for all other requests, data will be serialized as JSON and placed in the request body, sent asContent-Type: application/json. Default: no data will be sent.
Example: Perform multiple requests with different parameters.
r.expr([1, 2, 3]).map(function(i) {
return r.http('http://httpbin.org/get', { params: { user: i } });
}).run(conn, callback)Example: Perform a PUT request for each item in a table.
r.table('data').map(function(row) {
return r.http('http://httpbin.org/put', { method: 'PUT', data: row });
}).run(conn, callback)Example: Perform a POST request with accompanying data.
Using form-encoded data:
r.http('http://httpbin.org/post',
{ method: 'POST', data: { player: 'Bob', game: 'tic tac toe' } })
.run(conn, callback)Using JSON data:
r.http('http://httpbin.org/post',
{ method: 'POST',
data: r.expr(value).coerceTo('string'),
header: { 'Content-Type': 'application/json' } })
.run(conn, callback)r.http supports depagination, which will request multiple pages in a row and aggregate the results into a stream. The use of this feature is controlled by the optional arguments page and pageLimit. Either none or both of these arguments must be provided.
page: This option may specify either a built-in pagination strategy (see below), or a function to provide the next URL and/orparamsto request.pageLimit: An integer specifying the maximum number of requests to issue using thepagefunctionality. This is to prevent overuse of API quotas, and must be specified withpage.-1: no limit0: no requests will be made, an empty stream will be returnedn:nrequests will be made
At the moment, the only built-in strategy is 'link-next', which is equivalent to function(info) { return info('header')('link')('rel="next"').default(null); }.
Example: Perform a GitHub search and collect up to 3 pages of results.
r.http("https://api.github.com/search/code?q=addClass+user:mozilla",
{ page: 'link-next', pageLimit: 3 }
).run(conn, callback)As a function, page takes one parameter, an object of the format:
{
params: object // the URL parameters used in the last request
header: object // the HTTP headers of the last response as key/value pairs
body: value // the body of the last response in the format specified by `resultFormat`
}The header field will be a parsed version of the header with fields lowercased, like so:
{
'content-length': '1024',
'content-type': 'application/json',
'date': 'Thu, 1 Jan 1970 00:00:00 GMT',
'link': {
'rel="last"': 'http://example.com/?page=34',
'rel="next"': 'http://example.com/?page=2'
}
}The page function may return a string corresponding to the next URL to request, null indicating that there is no more to get, or an object of the format:
{
url: string // the next URL to request, or null for no more pages
params: object // new URL parameters to use, will be merged with the previous request's params
}Example: Perform depagination with a custom page function.
r.http('example.com/pages',
{ page: function(info) { return info('body')('meta')('next').default(null); },
pageLimit: 5 })
.run(conn, callback)See the tutorial on r.http for more examples on how to use this command.