HTTP JavaScript API
Connector functions HTTP JS API guide
This API allows users to execute HTTP related actions from a connector function.
HTTP Object
The http
object is the main object of the HTTP client. It allows perform different actions such as do http request or define http response for Operation Response or Iot Collections requests through http protocol.
http
object is divided in two objects:
server
: Only for CFs called from http requests. Gives access to received request and allows to specify http response to be sent.client
: Configure and make http requests.
server Object Properties
Read only properties with received HTTP request and response
object to define HTTP response to be sent.
Property | Type | Default | Description |
---|---|---|---|
headers | JSON | Received http request headers | |
uri | string | Received http request uri (without host) | |
body | * | Received http request body (the same content as the payload property) |
|
response | JSON | {} | Object to be used to define http response to be sent when CF finishes |
server.response Object Properties
Property | Type | Default | Description |
---|---|---|---|
status | integer | 201 | Response HTTP code |
body | * | null | Response body |
headers | JSON | null | Response HTTP headers |
server.response Object Methods
server.response.send()
Creates HTTP Response with defined properties. The response will be sent once the CF is finished correctly.
Example:
http.server.response.status=200;
http.server.response.body={'msg': 'OK'};
http.server.response.send();
//continue CF execution
return collection;
In this case, after CF execution, the Http response to be generated will have 200 status code with specified body ({'msg': 'OK'}
) and no specific headers.
client Object Properties
Property | Type | Default | Description |
---|---|---|---|
method | string | null | One of http methods: POST, GET, … |
uri | string | null | Request uri |
headers | JSON | null | Request headers in json format |
body | * | null | Response body |
alias | string | null | Used to set custom https context, alias to be used for keystore |
certificate | string | null | Used to set custom https context, certificate content |
privateKey | string | null | Used to set custom https context, private key content |
redirectPolicy | string | null | Overrides default redirection policy |
clientVersion | string | null | Overrides default client version configured |
timeOut | integer | null | Overrides default timeout configured. Defined in seconds |
client Object Methods
Following methods return Http Request result JSON with these fields:
Field | Description |
---|---|
statusCode | Received response HTTP code |
body | Received response body |
headers | Received response headers |
client.post()
Performs POST using defined configuration. Overrides defined method
.
Example of use with default values:
http.client.uri = "https://remotehost/uri";
http.client.headers = {'X-Api-Key':'apikey'};
http.client.body = {'operation_name':'custom_operation_request'};
var httpResp = http.client.post();
client.put()
Performs PUT using defined configuration. Overrides defined method
.
Example of use with default values:
http.client.uri = "https://remotehost/uri";
http.client.headers = {'X-Api-Key':'apikey'};
http.client.body = {'operation_name':'custom_operation_request'};
var httpResp = http.client.put();
client.get()
Performs GET using defined configuration. Overrides defined method
.
Example of use with default values:
http.client.uri = "https://remotehost/uri";
http.client.headers = {'X-Api-Key':'apikey'};
var httpResp = http.client.get();
client.delete()
Performs DELETE using defined configuration. Overrides defined method
.
Example of use with default values:
http.client.uri = "https://remotehost/uri";
http.client.headers = {'X-Api-Key':'apikey'};
var httpResp = http.client.delete();
client.patch()
Performs PATCH using defined configuration. Overrides defined method
.
Example of use with default values:
http.client.uri = "https://remotehost/uri";
http.client.headers = {'X-Api-Key':'apikey'};
http.client.body = {'operation_name':'custom_operation_request'};
var httpResp = http.client.patch();
client.request()
Performs configured http request. method
property must be defined.
Example of use with default values:
http.client.method = "GET";
http.client.uri = "https://remotehost/uri";
http.client.headers = {'X-Api-Key':'apikey'};
http.client.body = {'operation_name':'custom_operation_request'};
var httpResp = http.client.request();