GET
|
/**
HTTP GET: Fetch all items
Signature: http.get(url:
string, headers: object,
callback: function)
Example: Get list of all
items
Expected: 200 OK with
array of items
*/
|
function
getAllItemsCallback(err,
status, body, headers) {
if (err) {
logger.error("[GET ALL]
Error:", err.message);
} else {
logger.info("[GET ALL]
Success:", status,
body);
}
}
http.get(
"http://localhost:3000/a
pi/data",
{ "Accept":
"application/json" },
getAllItemsCallback
);
|
POST
|
/*** HTTP POST: Create new
item* Signature:
http.post(url: string,
body: object|string,
headers: object, callback:
function)* Example: Create new
item with name "Item
Three"
* Expected: 201 Created
*/
|
function
createItemCallback(err,
status, body, headers) {
if (err) {
logger.error("[POST]
Error:", err.message);
} else {
logger.info("[POST]
Success:", status,
body);
}
}
http.post(
"http://localhost:3000/a
pi/data",
{ name: "Item Three"
},
{ "Content-Type":
"application/json" },
createItemCallback
);
|
DELETE
|
/**
* HTTP DELETE: Delete
item by ID
* Signature:
http.delete(url: string,
callback: function)
* Example: Delete item
with ID 2
* Expected: 200 OK or 404
Not Found
*/
|
function
deleteItemCallback(err,
status, body, headers) {
if (err) {
logger.error("
[DELETE] Error:",
err.message);
} else {
logger.info("[DELETE]
Success:", status, body);
}
}
http.delete("http://local
host:3000/api/data/2",
deleteItemCallback);
|
PUT
|
/**
* HTTP PUT: Update item
by ID
* Signature:
http.put(url: string,
body: object|string,
headers: object, callback:
function)
* Example: Update item ID
1 to have a new name
* Expected: 200 OK or 404
Not Found
*/
|
function
updateItemCallback(err,
status, body, headers) {
if (err) {
logger.error("[PUT]
Error:", err.message);
} else {
logger.info("[PUT]
Success:", status,
body);
}
}
http.put(
"http://localhost:3000/a
pi/data/1",
{ name: "Updated Item
One" },
{ "Content-Type":
"application/json" },
updateItemCallback
);
|
SSL Options
|
/**
* SSL Configuration
Example (optional)
* Signature:
http.setSSLOptions(options
: object)
* Example: Enable selfsigned
certificate support
* Use this before calling
HTTPS endpoints
*/
|
http.setSSLOptions({
verify: true,
verifyHostname: true,
//
allowSelfSigned: true,
//
});
|
SSL options with Certificates
|
Certificates and keys can also be provided via the customAttribute file options. These values can be integrated into the data logic. Before being passed to setSslOptions , the certificate files must be base64- encoded, as demonstrated in the provided example.
|
function
decodeFileContent(fileAt
tribute) {
// File attributes
come as binary data,
need to decode to string
var decoder = new
TextDecoder();
return
decoder.decode(fileAttri
bute);
}
var caCert =
decodeFileContent(input.
ca_certificate);
var clientCert =
decodeFileContent(input.
client_certificate);
var clientKey =
decodeFileContent(input.
client_key);
var sslOptions = {
verify: true,
caFile: caCert, //
root certificate
verifyHostname:
false,
certFile:
clientCert, // client
certificate
keyFile: clientKey
// client / private key
};
var
sslConfigured =
http.setSSLOptions(sslOp
tions);
|
Timeout
|
/**
Timeout Configuration
Signature:
http.setTimeout(timeoutMs:
number)
Example: Set timeout to
2000 ms
*/
|
http.setTimeout(2000); //
Set timeout for all
requests to 2 seconds
MIN_TIMEOUT =1000; // 1 second
MAX_TIMEOUT =300000; // 5 minutes
DEFAULT_TIMEOUT = 8000; // 8 seconds
|
Connection Reuse
|
/*
http.setConnectionReuse(enabled
: boolean): boolean
Controls whether HTTP
connections are reused (pooled)
or closed after each request.
✅ Parameters
enabled (boolean):
true: Enables connection reuse
(default behavior)
false: Disables connection
reuse — HTTP connection will be
closed after each request
🔁 Returns
true on success
false if setting failed (e.g.,
unsupported in the current
environment)*/
|
function init() {
http.setConnectionReuse(true); // by dfault its true mentioned this .
}
|