Sample code for 30+ languages & platforms
Delphi ActiveX

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

Delphi ActiveX
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Chilkat_TLB;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Integer;
sbTargetCurl: TChilkatStringBuilder;
httpCurl: TChilkatHttpCurl;
responseJson: TChilkatJsonObject;
statusCode: Integer;
targetCurl: WideString;

begin
success := 0;

// 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 := TChilkatStringBuilder.Create(Self);
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 := TChilkatHttpCurl.Create(Self);

// Run the curl command.
success := httpCurl.DoYourThing(sbTargetCurl.GetAsString());
if (success = 0) then
  begin
    Memo1.Lines.Add(httpCurl.LastErrorText);
    Exit;
  end;

responseJson := TChilkatJsonObject.Create(Self);
responseJson.EmitCompact := 0;

httpCurl.GetResponseJson(responseJson.ControlInterface);

statusCode := httpCurl.StatusCode;
Memo1.Lines.Add('response status code: ' + IntToStr(statusCode));

Memo1.Lines.Add(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 = 0) then
  begin
    Memo1.Lines.Add(httpCurl.LastErrorText);
    Exit;
  end;

httpCurl.GetResponseJson(responseJson.ControlInterface);

statusCode := httpCurl.StatusCode;
Memo1.Lines.Add('response status code: ' + IntToStr(statusCode));

Memo1.Lines.Add(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"
// }
end;