Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
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 Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.StringBuilder,
Chilkat.Rest,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
rest: TRest;
bAutoReconnect: Boolean;
json: TJsonObject;
sb: TStringBuilder;
sbResp: TStringBuilder;
begin
success := False;
success := False;
rest := TRest.Create;
// Connect using TLS.
bAutoReconnect := True;
success := rest.Connect('chilkatsoft.com',443,True,bAutoReconnect);
// Load JSON containing the following Korean text.
// {
// "BillAddr": {
// "Id": "239615",
// "Line1": "류리하",
// "Line2": "류리하류리하",
// "City": "류리하류리하",
// "Country": "US",
// "CountrySubDivisionCode": "AK",
// "PostalCode": "류리하"
// }
// }
json := TJsonObject.Create;
json.EmitCompact := False;
success := json.LoadFile('qa_data/json/korean.json');
if (success = False) then
begin
WriteLn(json.LastErrorText);
Exit;
end;
success := rest.AddHeader('Content-Type','application/json; charset=UTF-8');
sb := TStringBuilder.Create;
json.EmitSb(sb);
sb.Encode('unicodeescape','utf-8');
WriteLn(sb.GetAsString());
// 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 := TStringBuilder.Create;
success := rest.FullRequestSb('POST','/echo_request_body.asp',sb,sbResp);
if (success = False) then
begin
WriteLn(rest.LastErrorText);
Exit;
end;
// Show the response.
WriteLn('Json Response: ' + sbResp.GetAsString());
rest.Free;
json.Free;
sb.Free;
sbResp.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.