RESTful calls from Node++
Node++ provides a simple RESTful calls facility. A couple of macros handle JSON objects and HTTP calls.
There are two macros for making calls:
CALL_HTTP – make an HTTP call with plain character buffers
CALL_REST – make an HTTP call with buffers treated as JSONs
They both use the same macros for after-call checks:
- CALL_HTTP_STATUS
- CALL_HTTP_CONTENT_TYPE
- CALL_HTTP_STATUS_OK
- CALL_HTTP_RESPONSE_LEN
CALL_HTTP_HEADERS_RESET;
CALL_HTTP_HEADER_SET("apiConsumerId", "123456789");
CALL_HTTP_HEADER_SET("Authorization", "Bearer v2-6998a852-1521-4745-a1f3-e55341092e9f");
JSON json_req={0};
JSON json_res={0};
JSON_ADD_STR(&json_req, "accountNo", "987654321");
if ( !CALL_REST(&json_req, &json_res, "POST", "https://example.com:8888/api/v1.0/accounts/getAccount", false) )
{
// TCP level error, i.e. couldn't connect to the remote host
ERR("Couldn't connect to example.com");
OUT("<p>Couldn't connect to example.com</p>");
return ERR_REMOTE_CALL;
}
else if ( !CALL_HTTP_STATUS_OK )
{
// HTTP level error
WAR("getAccount status %d", CALL_HTTP_STATUS);
OUT("<p><code>%s</code></p>", JSON_TO_STRING_PRETTY(&json_res));
return ERR_REMOTE_CALL_STATUS;
}
// OK
char name[256];
if ( !JSON_GET_STR(&json_res, "name", name, 255) )
WAR("Couldn't get name from response");
else
OUT("<p>Name: %s</p>", name);
float balance;
if ( !JSON_GET_FLOAT(&json_res, "balance", &balance) )
WAR("Couldn't get balance from response");
else
OUT("<p>Balance: %.2f</p>", balance);
return OK;
-
CALL_HTTP may block for up to callHTTPTimeout (in milliseconds). If not set, then CALL_HTTP_DEFAULT_TIMEOUT is used, which is currently 10000 ms.
-
Maximum HTTP/REST call response length is CALL_HTTP_MAX_RESPONSE_LEN, by default 1 MiB.
-
Maximum JSON string length is NPP_JSON_BUFSIZE-1, by default 64 KiB.
-
Maximum JSON elements in one JSON structure is NPP_JSON_MAX_ELEMS, by default 15.
-
Maximum JSON nesting levels is NPP_JSON_MAX_LEVELS, by default 4.
-
There is NPP_JSON_POOL_SIZE (currently 1000) for JSON sub-records internal storage.
All the above can be in/de-creased, depending on your needs and available memory.