CLEAR

NetworkingModule


The NetworkingModule class enables effects to fetch information from a network. To use the Networking class you must add the Networking capability to the project manifest. You also need to whitelist domains you'd like to access in the Capabilities menu. AR Studio requires the URL passed to the fetch() method to be an HTTPS URL, with a certificate that chains up to a trusted certificate authority. Self-signed and other non-trusted certificates should not be expected to work.

Example

// Loading in the required modules
const Scene = require('Scene');
const Networking = require('Networking');

// Locate text node we'll use for displaying network request results.
const textNode = Scene.root.find('text0');

// Create constants with our request data.
const url = 'https://jsonplaceholder.typicode.com/posts';
const request = {
    method: 'POST',
    body: JSON.stringify({title: 'Networking Module'}),
    headers: {'Content-type': 'application/json; charset=UTF-8'}
}

// Perform network request with Networking module
Networking.fetch(url, request).then(function(result) {
    // The result object contains information about the request,
    // such as status code and response body.
    if ((result.status >= 200) && (result.status < 300)) {
        // If the request was successful, we'll chain the JSON forward
        return result.json();
    }
    // If the request was not successful, we should throw an error
    throw new Error("HTTP status code " + result.status);
}).then(function(json) {
    // Here we can process the JSON obtained by the successful request
    textNode.text = json.title;
}).catch(function(error) {
    // Here we process any errors that may happen with the request
    textNode.text = error.message;
});

Properties

This class exposes no properties.

Methods

MethodDescription

fetch

fetch(url: String) : Promise<ResponseObject> Returns a promise for the result of the call. A then() clause attached to this takes a single argument; that argument is a callback function which takes a single argument, which is the result of the call. The result of the call is an object with two elements: A status property indicating the HTTP response status of the call, and json() function, which returns a promise for the dictionary containing the body of the response (which is expected to be JSON).