Sample code for 30+ languages & platforms
Unicode C

POST JSON to REST API with non-us-ascii Chars Escaped

See more REST Examples

Demonstrates how to POST to a REST API with non-usascii chars within JSON Unicode escaped.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkRestW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkRestW rest;
    BOOL bAutoReconnect;
    HCkJsonObjectW json;
    HCkStringBuilderW sb;
    HCkStringBuilderW sbResp;

    success = FALSE;

    success = FALSE;

    rest = CkRestW_Create();

    //  Connect using TLS.
    bAutoReconnect = TRUE;
    success = CkRestW_Connect(rest,L"chilkatsoft.com",443,TRUE,bAutoReconnect);

    //  Load JSON containing the following Korean text.

    //   {
    //     "BillAddr": {
    //        "Id": "239615",
    //        "Line1": "류리하",
    //        "Line2": "류리하류리하",
    //        "City": "류리하류리하",
    //        "Country": "US",
    //        "CountrySubDivisionCode": "AK",
    //        "PostalCode": "류리하"
    //      }
    //  }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_putEmitCompact(json,FALSE);
    success = CkJsonObjectW_LoadFile(json,L"qa_data/json/korean.json");
    if (success == FALSE) {
        wprintf(L"%s\n",CkJsonObjectW_lastErrorText(json));
        CkRestW_Dispose(rest);
        CkJsonObjectW_Dispose(json);
        return;
    }

    success = CkRestW_AddHeader(rest,L"Content-Type",L"application/json; charset=UTF-8");

    sb = CkStringBuilderW_Create();
    CkJsonObjectW_EmitSb(json,sb);
    CkStringBuilderW_Encode(sb,L"unicodeescape",L"utf-8");

    wprintf(L"%s\n",CkStringBuilderW_getAsString(sb));

    //  The StringBuilder contains this:

    //  {
    //    "BillAddr": {
    //      "Id": "239615",
    //      "Line1": "\ub958\ub9ac\ud558",
    //      "Line2": "\ub958\ub9ac\ud558\ub958\ub9ac\ud558",
    //      "City": "\ub958\ub9ac\ud558\ub958\ub9ac\ud558",
    //      "Country": "US",
    //      "CountrySubDivisionCode": "AK",
    //      "PostalCode": "\ub958\ub9ac\ud558"
    //    }
    //  }

    sbResp = CkStringBuilderW_Create();
    success = CkRestW_FullRequestSb(rest,L"POST",L"/echo_request_body.asp",sb,sbResp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sb);
        CkStringBuilderW_Dispose(sbResp);
        return;
    }

    //  Show the response. 
    wprintf(L"Json Response: %s\n",CkStringBuilderW_getAsString(sbResp));


    CkRestW_Dispose(rest);
    CkJsonObjectW_Dispose(json);
    CkStringBuilderW_Dispose(sb);
    CkStringBuilderW_Dispose(sbResp);

    }