Making HTTP Requests from Cloud Code

Making HTTP Requests from Cloud Code

Cloud code is great for data validation, security enhancements, and altering the responses on the server based on custom logic. All those possibilities make it a really powerful feature. In Telerik Platform, you can also make external HTTP requests to retrieve data from external systems or implement various integration scenarios. This feature allows you to call any HTTP service with custom data, process the response and act based on it.

Sample Use Cases

There are countless possible scenarios where HTTP calls will be of great value to your system. Here is a very brief set of common scenarios that illustrate how you can use this feature in your app:

  • 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 etc.

Making a simple request

Here is an example of how to make a simple HTTP request from your cloud code:

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

Here is an explanation of each parameter:

  • method - the request method to use. This must be a valid HTTP method such as GET, POST, etc.
  • url - the URL to call.
  • options - object, containing suitable to the request data:
    • params - parameters for the requests (hashSet {foo: 'bar', baz: 'qux'}, defaultValue: {})
    • headers - any custom headers (hashSet {foo: 'bar', baz: 'qux'}, defaultValue: null)
    • body - request's body (defaultValue: null)
    • contentType - Content-Type header of the requests (defaultValue: null)
    • accepts - Accepts header of the requests (defaultValue: 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. (defaultValue: true)
  • callback - function that is invoked on after the request completes. The arguments are respectively error and result. If the request is successful, error will be null.

Real world example:

Everlive.Events.beforeRead(function (request, context, done) {
    Everlive.Http.request('GET',
        'http://en.wikipedia.org/w/api.php?action=query&titles=Albert%20Einstein&prop=info&format=json',
        null, // request options
        function (error, response) {
            if (error) {
                //Do something with the error here
            } else {
                //Do something with the response here
            }
            done();
        });
});

The error object has the following fields:

  • message - message, describing the error.
  • code - error code of the response.
  • name - name of the error.

The response 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 JavaScript object. This field is set only if the Content-Type is recognized and supported format.
  • headers - the headers of the response as a JavaScript object. Example: response.headers['Content-Type']

Convenience Methods

A few alternative methods for making HTTP requests are also available. They use the generic request method internally and are meant only for convenience, based on the type of the HTTP request. Here they are:

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

Things to Consider

While HTTP calls are a great feature, they must be used with caution. For example, if you make HTTP call for every item that gets created for certain content type and wait to process the response, this might significantly slow down your app.

The cloud code execution is time restricted. A single event execution can last no longer than 5 seconds. If this happens, the request will fail and return a timeout error to the client. Too many timeouts for a limited time interval will result in disabling the cloud code execution for this content type automatically by the cloud infrastructure. Trying to call the cloud code for a type will return a "Custom cloud code is disabled for this type" error from the server.

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.