Skip to content

CALL_REST

Jurek Muszyński edited this page Sep 11, 2018 · 4 revisions

bool CALL_REST(void *req, void *res, const char *method, const char *url)

Description

Calls RESTful service. req and res are JSON objects. CALL_REST can block for up to RESTTimeout milliseconds. If not set, CALL_REST_DEFAULT_TIMEOUT is used, which is currently 1000 ms.

CALL_REST and most of JSON_* macros have two versions:

By default they add ampersand to JSON arguments to make coding easier — the code then looks like in the example below. If JSON_NO_AUTO_AMPERSANDS compilation option is set, ampersands are not added, which may be preferred for seasoned C coders and/or there is frequent JSON arguments passing to functions.

For more information see RESTful calls from Silgy.

Returns

true if the full response content was read successfully, otherwise false. res will contain a JSON object from parsed response body.

Example

REST_HEADERS_RESET;

REST_HEADER_SET("apiConsumerId", "123456789");
REST_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", "example.com:8888/api/v1.0/accounts/getAccount") )
{
    OUT("<p>Got response</p>");
    // show the whole received record
    OUT("<p style=\"font-family:monospace; font-size:10pt; white-space:pre;\">%s</p>", JSON_TO_STRING_PRETTY(json_res));
    // show some fields
    OUT("<p>Name: %s</p>", JSON_GET_STR(json_res, "name"));
    OUT("<p>Balance: %.2f</p>", JSON_GET_FLOAT(json_res, "balance"));
}
else
{
    OUT("<p>Something went wrong</p>");
}
Clone this wiki locally