Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
curl POST with JSON Input and JSON Output
See more CURL Examples
Demonstrates running a simple curl command with JSON input and JSON output.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.HttpCurl,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
sbTargetCurl: TStringBuilder;
httpCurl: THttpCurl;
responseJson: TJsonObject;
statusCode: Integer;
targetCurl: string;
begin
success := False;
// Run the following curl command
// curl -X POST https://httpbin.org/post \
// -H "Content-Type: application/json" \
// -d '{
// "title": "foo",
// "body": "bar",
// "userId": 1
// }'
// The backslashes at the end of lines are not required. Chilkat ignores them if present.
sbTargetCurl := TStringBuilder.Create;
sbTargetCurl.AppendLn(' curl -X POST https://httpbin.org/post \");
sbTargetCurl.AppendLn(' -H "Content-Type: application/json" \");
sbTargetCurl.AppendLn(' -d ''{');
sbTargetCurl.AppendLn(' "title": "foo",');
sbTargetCurl.AppendLn(' "body": "bar",');
sbTargetCurl.AppendLn(' "userId": 1');
sbTargetCurl.AppendLn(' }''');
httpCurl := THttpCurl.Create;
// Run the curl command.
success := httpCurl.DoYourThing(sbTargetCurl.GetAsString());
if (success = False) then
begin
WriteLn(httpCurl.LastErrorText);
Exit;
end;
responseJson := TJsonObject.Create;
responseJson.EmitCompact := False;
httpCurl.GetResponseJson(responseJson);
statusCode := httpCurl.StatusCode;
WriteLn('response status code: ' + statusCode);
WriteLn(responseJson.Emit());
// Output:
// response status code: 200
// {
// "args": {},
// "data": "{\r\n \"title\": \"foo\",\r\n \"body\": \"bar\",\r\n \"userId\": 1\r\n }",
// "files": {},
// "form": {},
// "headers": {
// "Content-Length": "96",
// "Content-Type": "application/json",
// "Host": "httpbin.org",
// "X-Amzn-Trace-Id": "Root=1-69e8db8b-459b3bdf7b7a3bc749184968"
// },
// "json": {
// "body": "bar",
// "title": "foo",
// "userId": 1
// },
// "origin": "123.222.222.222",
// "url": "https://httpbin.org/post"
// }
// ----------------------------------------------------------------------------------
// Another example:
// curl -X POST https://postman-echo.com/post \
// -H "Content-Type: application/json" \
// -d '{"foo":"bar"}'
targetCurl := 'curl -X POST https://postman-echo.com/post -H "Content-Type: application/json" -d ''{"foo":"bar"}''';
// Run the curl command.
success := httpCurl.DoYourThing(targetCurl);
if (success = False) then
begin
WriteLn(httpCurl.LastErrorText);
Exit;
end;
httpCurl.GetResponseJson(responseJson);
statusCode := httpCurl.StatusCode;
WriteLn('response status code: ' + statusCode);
WriteLn(responseJson.Emit());
// Output:
// response status code: 200
// {
// "args": {},
// "data": {
// "foo": "bar"
// },
// "files": {},
// "form": {},
// "headers": {
// "host": "postman-echo.com",
// "content-length": "13",
// "content-type": "application/json",
// "x-forwarded-proto": "https",
// "accept-encoding": "gzip, br"
// },
// "json": {
// "foo": "bar"
// },
// "url": "https://postman-echo.com/post"
// }
sbTargetCurl.Free;
httpCurl.Free;
responseJson.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.