Sample code for 30+ languages & platforms
Delphi DLL Requires Chilkat v11.1.0+

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 Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
rest: HCkRest;
bAutoReconnect: Boolean;
json: HCkJsonObject;
sb: HCkStringBuilder;
sbResp: HCkStringBuilder;

begin
success := False;

success := False;

rest := CkRest_Create();

//  Connect using TLS.
bAutoReconnect := True;
success := CkRest_Connect(rest,'chilkatsoft.com',443,True,bAutoReconnect);

//  Load JSON containing the following Korean text.

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

json := CkJsonObject_Create();
CkJsonObject_putEmitCompact(json,False);
success := CkJsonObject_LoadFile(json,'qa_data/json/korean.json');
if (success = False) then
  begin
    Memo1.Lines.Add(CkJsonObject__lastErrorText(json));
    Exit;
  end;

success := CkRest_AddHeader(rest,'Content-Type','application/json; charset=UTF-8');

sb := CkStringBuilder_Create();
CkJsonObject_EmitSb(json,sb);
CkStringBuilder_Encode(sb,'unicodeescape','utf-8');

Memo1.Lines.Add(CkStringBuilder__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 := CkStringBuilder_Create();
success := CkRest_FullRequestSb(rest,'POST','/echo_request_body.asp',sb,sbResp);
if (success = False) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

//  Show the response. 
Memo1.Lines.Add('Json Response: ' + CkStringBuilder__getAsString(sbResp));

CkRest_Dispose(rest);
CkJsonObject_Dispose(json);
CkStringBuilder_Dispose(sb);
CkStringBuilder_Dispose(sbResp);