lib/xp/http-client

HTTP Client related functions.

Example
var httpClientLib = require('/lib/xp/http-client');

Methods

(static) request(params) → {Response}

Sends an HTTP request and returns the response received from the remote server. The request is sent synchronously, the execution blocks until the response is received.

Parameters:
Name Type Description
params object

JSON parameters.

Properties
Name Type Attributes Default Description
url string

URL to which the request is sent.

method string <optional>
GET

The HTTP method to use for the request (e.g. "POST", "GET", "HEAD", "PUT", "DELETE").

params object <optional>

Query or form parameters to be sent with the request.

headers object <optional>

HTTP headers, an object where the keys are header names and the values the header values.

connectionTimeout number <optional>
10000

The timeout on establishing the connection, in milliseconds.

readTimeout number <optional>
10000

The timeout on waiting to receive data, in milliseconds.

body string | * <optional>

Body content to send with the request, usually for POST or PUT requests. It can be of type string or stream.

contentType string <optional>

Content type of the request.

multipart Array.<object> <optional>

Multipart form data to send with the request, an array of part objects. Each part object contains 'name', 'value', and optionally 'fileName' and 'contentType' properties. Where 'value' can be either a string or a Stream object.

proxy object <optional>

Proxy settings.

Properties
Name Type Attributes Description
host string <optional>

Proxy host name to use.

port number <optional>

Proxy port to use.

user string <optional>

User name for proxy authentication.

password string <optional>

Password for proxy authentication.

Returns:

response HTTP response received.

Type
Response
Examples
var response = httpClientLib.request({
    url: 'http://' + getServerHost() + '/my/service',
    method: 'POST',
    headers: {
        'X-Custom-Header': 'header-value'
    },
    connectionTimeout: 20000,
    readTimeout: 5000,
    body: '{"id": 123}',
    contentType: 'application/json'
});
// Expected result from request.
var expected = {
    'status': 200,
    'message': 'OK',
    'body': 'POST request',
    'contentType': 'text/plain',
    'headers': {
        'Content-Length': '12',
        'content-type': 'text/plain'
    }
};
var response = httpClientLib.request({
    url: 'http://' + getServerHost() + '/uploadMedia',
    method: 'POST',
    contentType: 'multipart/mixed',
    multipart: [
        {
            name: 'media',
            fileName: 'logo.png',
            contentType: 'image/png',
            value: myImageStream
        },
        {
            name: 'category',
            value: 'images'
        }
    ]
});
// request using a proxy with authentication
var response = httpClientLib.request({
    url: 'http://' + host + '/some/service',
    method: 'GET',
    proxy: {
        host: '172.16.0.42',
        port: 8080,
        user: 'admin',
        password: 'secret'
    }
});

Type Definitions

Response

Type:
  • Object
Properties:
Name Type Description
status number

HTTP status code returned.

message string

HTTP status message returned.

headers object

HTTP headers of the response.

contentType string

Content type of the response.

body string

Body of the response as string. Null if the response content-type is not of type text.

bodyStream *

Body of the response as a stream object.