Unicode C
Unicode C
Send a REST Request with a Text Body
See more REST Examples
Demonstrates Rest.FullRequestString, a convenience method that sends a complete request whose text body (such as JSON, XML, or plain text) is supplied directly, then reads and returns the response body as text.
Background. The full-request convenience methods perform the entire request/response exchange in a single call. FullRequestString is well suited to typical JSON or XML API calls where the body fits comfortably in a string.
Chilkat Unicode C Downloads
#include <C_CkRestW.h>
void ChilkatSample(void)
{
BOOL success;
HCkRestW rest;
BOOL bTls;
BOOL bAutoReconnect;
const wchar_t *jsonBody;
const wchar_t *responseText;
success = FALSE;
rest = CkRestW_Create();
bTls = TRUE;
bAutoReconnect = TRUE;
success = CkRestW_Connect(rest,L"example.com",443,bTls,bAutoReconnect);
if (success == FALSE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkRestW_Dispose(rest);
return;
}
// Send a complete request whose text body is supplied directly, such as JSON. The response body is
// read as text and returned. The 1st argument is the HTTP verb, the 2nd is the URI path, and the
// 3rd is the request body.
CkRestW_AddHeader(rest,L"Content-Type",L"application/json");
jsonBody = L"{ \"name\": \"example\", \"active\": true }";
responseText = CkRestW_fullRequestString(rest,L"POST",L"/api/items",jsonBody);
if (CkRestW_getLastMethodSuccess(rest) == FALSE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkRestW_Dispose(rest);
return;
}
wprintf(L"%s\n",responseText);
CkRestW_Dispose(rest);
}