-
Notifications
You must be signed in to change notification settings - Fork 4
CALL_REST
Jurek Muszyński edited this page Mar 31, 2022
·
9 revisions
Makes HTTP request. req and res are JSON* pointers. CALL_REST can block for up to callHTTPTimeout milliseconds. If not set, CALL_HTTP_DEFAULT_TIMEOUT is used, which is currently 10000 ms.
keep is for keeping connection open after request. When no longer needed, it can be closed with CALL_HTTP_DISCONNECT.
For more information see RESTful calls from Node++.
true if the full response content was read successfully, otherwise false. res will contain a NULL-terminated string with response body.
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;