Making HTTP Requests from Cloud Code

The Cloud Code SDK comes with utility methods for making external HTTP requests, which are useful for retrieving data from external systems or implementing various integration scenarios. They allow you to call any HTTP service with custom data, process the response, and act on it.

Some possible scenarios for using HTTP calls include:

  • Send a notification email on successful user registration.
  • Notify other systems about events in your app such as Likes of their posts.
  • Notify administrators for important events such as Errors.

In his article:

Syntax

Use the Cloud Code SDK HTTP.request() method to make an HTTP request from your Cloud Code:

Everlive.Http.request(method, url, options, callback);

Where:

  • method—Request method to use. Must be a valid HTTP verb such as GET, POST, and so on.
  • url—URL to call.
  • options—An object containing data specific to the requested URL:
    • params—An HTTP query string parameters which will be concatenated to the url in ?paramA=value&paramB=value format. Default value: {}. Example: {foo: 'bar', baz: 'qux'}
    • headers—An object containing custom headers. Default value: null. Example: {X-Everlive-Skip: '3', X-Everlive-Take: '10'}
    • body—An object containing the request body. Default value: null.
    • contentTypeContent-Type header of the requests. Default value: null.
    • acceptsAccept header of the requests. Default value: null.
    • rejectUnauthorized—Specifies whether to check the SSL certificate of the remote server against the list of certificate authorities. For example, set this to false when you need to communicate with a web service encrypted with a self-signed certificate. Default value: true.
  • callback—A function that is invoked after the request completes. It receives two arguments: one for the error and one for the result. If the request is successful, the first argument is null.
    • The error object has the following fields:
      • message—A message describing the error.
      • code—The error code of the response.
      • name—The name of the error.
    • The result object has the following fields:
      • statusCode—The HTTP status code of the response.
      • body—The body of the response as a string.
      • data—The body of the response as a JavaScript object. This field is set only if the Content-Type is recognized and supported.
      • headers—The headers of the response as a JavaScript object. Example: response.headers['Content-Type'].

Use HTTP calls from Cloud Code with caution. For example, if you make an HTTP call for every item that gets created for a content type and wait to process the response, this might significantly slow down your app. Also consider the execution restrictions: Restrictions of Cloud Code for Data and Restrictions of Cloud Functions.

For example, the following code issues a GET request by setting some URL parameters in the query string.

var method = "GET";
var url = "my-url";
var options = {};

options.params = {
    "pageSize": 10,
    "page": 4
}; //  will set the URL parameters like - ?pageSize=10&page=4

options.headers = {
    "Authorization": "bearer a896fa69fa9"
};

Everlive.Http.request(method,
    url, options,
    function(error, result) {
        if (error) {
            //Do something with the error here and call done
            console.log(error);
            done();
        } else {
            //Do something with the response here and call done
            done();
        }
    });

The following example issues a POST request by sending a JSON body with the request

var method = "POST";
var url = "my-url";
var options = {};

options.contentType = 'application/json';

options.body = {
    "baz": "qux"
};

options.headers = {
    "Authorization": "bearer a896fa69fa9"
};

Everlive.Http.request(method,
    url, options,
    function(error, result) {
        if (error) {
            //Do something with the error here and call done
            console.log(error);
            done();
        } else {
            //Do something with the response here and call done
            done();
        }
    });

Convenience Methods

A few alternative methods for making HTTP requests are available. Internally, they use the generic request method and can be more convenient for some HTTP request types. The full list includes:

Everlive.Http.get(url, options, callback);
Everlive.Http.post(url, options, callback);
Everlive.Http.delete(url, options, callback);
Everlive.Http.put(url, options, callback);

See Also

Start a free trial Request a demo
Contact us: +1-888-365-2779
sales@telerik.com
Copyright © 2016-2017, Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved.