HTTP

April 11, 2019 ยท View on GitHub

The HTTP service allows you to create HTTP calls and receive Javascript promises to manage your call response results.

All of Litespeed.js internal HTTP calls used by the framework are managed by the HTTP service.

Currently the HTTP service uses the XMLHttpRequest API to handle all HTTP calls but in future releases we do intend to start using the newer Fetch API.

API

get

Make and HTTP GET request

ParamTypeDescription
urlstringHTTP URL to the resource you want to request

Return Value

This method returns a Javascript Promise object.

Example

http.get('https://example.com')
    .then(function (response) {
            // Handle Success
        }, function (error) {
            // Handle Error
        }
    );

post

Make and HTTP POST request

ParamTypeDescription
urlstringHTTP URL to the resource you want to request
headersobjectHTTP headers object combined from key and values
payloadstringHTTP payload

Return Value

This method returns a Javascript Promise object.

Example

http.post('https://example.com/resource', {'Content-type': 'application/json'}, '{name: "John", age: 31, city: "New York"}')
    .then(function (response) {
            // Handle Success
        }, function (error) {
            // Handle Error
        }
    );

put

Make and HTTP PUT request

ParamTypeDescription
urlstringHTTP URL to the resource you want to request
headersobjectHTTP headers object combined from key and values
payloadstringHTTP payload

Return Value

This method returns a Javascript Promise object.

Example

http.put('https://example.com/resource', {'Content-type': 'application/json'}, '{name: "John", age: 31, city: "New York"}')
    .then(function (response) {
            // Handle Success
        }, function (error) {
            // Handle Error
        }
    );

patch

Make and HTTP PATCH request

ParamTypeDescription
urlstringHTTP URL to the resource you want to request
headersobjectHTTP headers object combined from key and values
payloadstringHTTP payload

Return Value

This method returns a Javascript Promise object.

Example

http.patch('https://example.com/resource', {'Content-type': 'application/json'}, '{name: "John", age: 31, city: "New York"}')
    .then(function (response) {
            // Handle Success
        }, function (error) {
            // Handle Error
        }
    );

delete

Make and HTTP DELETE request

ParamTypeDescription
urlstringHTTP URL to the resource you want to request

Return Value

This method returns a Javascript Promise object.

Example

http.delete('https://example.com/resource')
    .then(function (response) {
            // Handle Success
        }, function (error) {
            // Handle Error
        }
    );

addGlobalParam

Add a global query param to all your HTTP requests. The parameter you will be adding using this method will be attached to any HTTP request sent using the HTTP service.

ParamTypeDescription
keystringParameter name
valuestringParameter value

Return Value

Items New elements of the Array.

Example

http.addGlobalParam('version', 'beta');

addGlobalHeader

Add a global header to all your HTTP requests. The HEADER you will be adding using this method will be attached to any HTTP request sent using the HTTP service.

ParamTypeDescription
keystringHeader name
valuestringHeader value

Return Value

Items New elements of the Array.

Example

http.addGlobalHeader('Content-type', 'application/json');