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