Request Module

const request = require('@arangodb/request')

The request module provides the functionality for making HTTP requests.

Making HTTP requests

HTTP method helpers

In addition to the request function convenience shorthands are available for each HTTP method in the form of, i.e.:

  • request.head(url, options)
  • request.get(url, options)
  • request.post(url, options)
  • request.put(url, options)
  • request.delete(url, options)
  • request.patch(url, options)

These are equivalent to using the request function directly, i.e.:

  1. request[method](url, options)
  2. // is equivalent to
  3. request({method, url, ...options});

For example:

  1. const request = require('@arangodb/request');
  2. request.get('http://localhost', {headers: {'x-session-id': 'keyboardcat'}});
  3. // is equivalent to
  4. request({
  5. method: 'get',
  6. url: 'http://localhost',
  7. headers: {'x-session-id': 'keyboardcat'}
  8. });

The request function

The request function can be used to make HTTP requests.

request(options)

Performs an HTTP request and returns a Response object.

Parameter

The request function takes the following options:

  • url or uri: the fully-qualified URL or a parsed URL from url.parse.
  • qs (optional): object containing querystring values to be appended to the URL.
  • useQuerystring: if true, use querystring module to handle querystrings, otherwise use qs module. Default: false.
  • method (optional): HTTP method (case-insensitive). Default: "GET".
  • headers (optional): HTTP headers (case-insensitive). Default: {}.
  • body (optional): request body. Must be a string or Buffer, or a JSON serializable value if json is true.
  • json: if true, body will be serialized to a JSON string and the Content-Type header will be set to "application/json". Additionally the response body will also be parsed as JSON (unless encoding is set to null). Default: false.
  • form (optional): when set to a string or object and no body has been set, body will be set to a querystring representation of that value and the Content-Type header will be set to "application/x-www-form-urlencoded". Also see useQuerystring.
  • auth (optional): an object with the properties username and password for HTTP Basic authentication or the property bearer for HTTP Bearer token authentication.
  • sslProtocol (optional): which tls version should be used to connect to the url. The default is 4 which is TLS 1.0. See SSL protocol for more opitions.
  • followRedirect: whether HTTP 3xx redirects should be followed. Default: true.
  • maxRedirects: the maximum number of redirects to follow. Default: 10.
  • encoding: encoding to be used for the response body. If set to null, the response body will be returned as a Buffer. Default: "utf-8".
  • timeout: number of seconds to wait for a response before aborting the request.
  • returnBodyOnError: whether the response body should be returned even when the server response indicates an error. Default: true.

The function returns a Response object with the following properties:

  • rawBody: the raw response body as a Buffer.
  • body: the parsed response body. If encoding was not set to null, this is a string. If additionally json was set to true and the response body is well-formed JSON, this is the parsed JSON data.
  • headers: an object containing the response headers. Otherwise this is identical to rawBody.
  • statusCode and status: the HTTP status code of the response, e.g. 404.
  • message: the HTTP status message of the response, e.g. Not Found.

Forms

The request module supports application/x-www-form-urlencoded (URL encoded) form uploads:

  1. const request = require('@arangodb/request');
  2. var res = request.post('http://service.example/upload', {form: {key: 'value'}});
  3. // or
  4. var res = request.post({url: 'http://service.example/upload', form: {key: 'value'}});
  5. // or
  6. var res = request({
  7. method: 'post',
  8. url: 'http://service.example/upload',
  9. form: {key: 'value'}
  10. });

Form data will be encoded using the qs module by default.

If you want to use the querystring module instead, simply use the useQuerystring option.

JSON

If you want to submit JSON-serializable values as request bodies, just set the json option:

  1. const request = require('@arangodb/request');
  2. var res = request.post('http://service.example/notify', {body: {key: 'value'}, json: true});
  3. // or
  4. var res = request.post({url: 'http://service.example/notify', body: {key: 'value'}, json: true});
  5. // or
  6. var res = request({
  7. method: 'post',
  8. url: 'http://service.example/notify',
  9. body: {key: 'value'},
  10. json: true
  11. });

HTTP authentication

The request module supports both HTTP Basic authentication. Just pass the credentials via the auth option:

  1. const request = require('@arangodb/request');
  2. var res = request.get(
  3. 'http://service.example/secret',
  4. {auth: {username: 'jcd', password: 'bionicman'}}
  5. );
  6. // or
  7. var res = request.get({
  8. url: 'http://service.example/secret',
  9. auth: {username: 'jcd', password: 'bionicman'}
  10. });
  11. // or
  12. var res = request({
  13. method: 'get',
  14. url: 'http://service.example/secret',
  15. auth: {username: 'jcd', password: 'bionicman'}
  16. });

Alternatively you can supply the credentials via the URL:

  1. const request = require('@arangodb/request');
  2. var username = 'jcd';
  3. var password = 'bionicman';
  4. var res = request.get(
  5. 'http://' +
  6. encodeURIComponent(username) +
  7. ':' +
  8. encodeURIComponent(password) +
  9. '@service.example/secret'
  10. );

You can also use Bearer token authentication:

  1. const request = require('@arangodb/request');
  2. var res = request.get(
  3. 'http://service.example/secret',
  4. {auth: {bearer: 'keyboardcat'}}
  5. );
  6. // or
  7. var res = request.get({
  8. url: 'http://service.example/secret',
  9. auth: {bearer: 'keyboardcat'}
  10. });
  11. // or
  12. var res = request({
  13. method: 'get',
  14. url: 'http://service.example/secret',
  15. auth: {bearer: 'keyboardcat'}
  16. });