V8 Javascript API

Http Requests

[V8 API]

Description

Http Requests module. RealityServer allows http requests to be made via the http global object. This provides an interface to send one or more simultaneous requests and process their responses upon completion.

The simplest way to make a request is to provide http.get() with a string url:

‎const response = http.get('http://example.com/');
console.log(response.body);

Requests may also return errors, for instance if a domain cannot be resolved. In this case, an error object is returned instead of a response.

‎const response = http.get('http://example.com/');
if (response instanceof http.Error) {
    console.error(response.message);
}

JSON APIs are also supported by specifying json as true in your request object. In this mode the request body is now a JSON serialisable object and the response body will be automatically deserialised for you. Note that in this case we would like to send a http POST command and are using the http.post() function, but any of the http request functions may be used in its place.

‎const response = http.post({
    url : 'http://api.example.com/message',
    json : true,
    body : {
        account : 10,
        message : 'hello world'
    }
});
console.log(response.body.message_id);

Multiple requests may be sent concurrently by passing an array of requests to a http request function. In this case we are using http.send() (which is the same as http.get()) and providing an overridden POST method to our last request in the array. Note that multiple requests will all be sent at the same time and produce an array of results once they have all completed.

‎const responses = http.send([
    'http://example.com/',
    'http://example.com/resource/2',
    {
        method : 'post',
        url : 'http://api.example.com/message',
        json : true,
        body : {
            account : 10,
            message : 'hello world'
        }
    }
]);

for (let response of responses) {
    if (response instanceof http.Error) {
        console.error(response.error);
    }
    else {
        console.log(response.message);
    }
}

Binary uploads and downloads are supported via the ArrayBuffer class. Make sure to specify a null encoding in your request if you wish to receive a binary response, otherwise your response body will be decoded using utf-8 and returned as a string.

‎const response = http.get({
    url : 'http://example.com/example.jpg',
    encoding : null
});
fs.writeFile('example.jpg', response.body);

Uploading an image:

‎const image = fs.readFile('example.jpg');
const response = http.put({
    url : 'http://example.com/example.jpg',
    body : image.buffer,
    encoding : null
});

console.log(response.message);

In addition to the synchronous API described above there is an asynchronous API defined on http.async. This provides the same functionality except Promises are returned that resolve with the responses.

Classes

class 
More...
class 
More...
class 
More...
class 
More...
class 
Asynchronous variants of the http api. More...