How to Post JSON data

 
1. If you need to submit data in JSON format, you can use the following two components: TclHttp and TclHttpRequest.
procedure TForm1.Button1Click(Sender: TObject);
var
  json: string;
  response: TStrings;
begin
  json := '{"firstname": "John", "lastname": "Smith", "age": 33}';
  response := TStringList.Create();
  try
    clHttpRequest1.BuildJSONRequest(json);
    clHttpRequest1.Header.ContentType := 'application/json';
    clHttp1.Post('http.....', clHttpRequest1, response);

    json := response.Text;

    ShowMessage(json);
  finally
    response.Free();
  end;
end;

2. Alternatively, you can use the Clever Internet Suite Json parser to build and parse Json strings: TclJSONObject.
procedure TForm1.Button2Click(Sender: TObject);
var
  obj: TclJSONObject;
  response: TStrings;
begin
  response := TStringList.Create();
  obj := TclJSONObject.Create();
  try
    obj.AddString('firstname', 'John');
    obj.AddString('lastname', 'Smith');
    obj.AddValue('age', '33');
 
    clHttpRequest1.BuildJSONRequest(obj); 
    clHttp1.Post('http.....', clHttpRequest1, response);
 
    FreeAndNil(obj);
    obj := TclJSONObject.ParseObject(response.Text);
 
    ShowMessage(obj.ValueByName('account'));
  finally
    obj.Free();
    response.Free();
  end;
end;

3. If you want to serialize JSON data with Delphi objects, you can use JsonSerializer from the Delphi JSON serialization using Rtti article.
Don't forget to add the following units to your "uses" section:
uses
  ......, clJson, clJsonSerializerBase, clJsonSerializer;
 
type
  TPerson = class
  private
    FAge: Integer;
    FLastName: string;
    FFirstName: string;
  public
    [TclJsonString('firstname')]
    property FirstName: string read FFirstName write FFirstName;
    [TclJsonString('lastname')]
    property LastName: string read FLastName write FLastName;
    [TclJsonProperty('age')]
    property Age: Integer read FAge write FAge;
  end;
 
  TAccount = class
  private
    FName: string;
    FAccount: string;
  public
    [TclJsonString('name')]
    property Name: string read FName write FName;
    [TclJsonString('account')]
    property Account: string read FAccount write FAccount;
  end;
 
procedure TForm1.Button3Click(Sender: TObject);
var
  person: TPerson;
  account: TAccount;
  serializer: TclJsonSerializer;
  json: string;
  response: TStrings;
begin
  serializer := TclJsonSerializer.Create();
  response := TStringList.Create();
  person := TPerson.Create();
  try
    person.FirstName := 'John';
    person.LastName := 'Smith';
    person.Age := 33;
    json := serializer.ObjectToJson(person);
    clHttpRequest1.BuildJSONRequest(json); 
    clHttp1.Post('http.....', clHttpRequest1, response);
 
    json := response.Text;
 
    account := serializer.JsonToObject(TAccount, json) as TAccount;
    try
      ShowMessage(account.Account);
    finally
      account.Free();
    end;
  finally
    person.Free();
    response.Free();
    serializer.Free();
  end;
end;

You may need to set up the HTTP component before sending data.
 
//for user-password authorization
clHttp1.UserName := 'username';
clHttp1.Password := 'sectret';
 
//for oauth authorization
clOAuth1.AuthURL := 'https....';
clOAuth1.TokenURL := 'https....';
clOAuth1.RedirectURL := 'http....';
clOAuth1.ClientID := 'client-identifier';
clOAuth1.ClientSecret := 'client-secret';
clOAuth1.Scope := 'authorization-scope';
 
clHttp1.Authorization := clOAuth1.GetAuthorization();
 
//get extended http status codes
clHttp1.SilentHTTP := True;
 
//set the user agent
clHttp1.UserAgent := 'My sample application';

Feedback

Add Feedback
how to add custom header to the request ?
Ashraf F. M. Aljarba (September 12, 2019 at 12:29 AM)
You need to use the following code:

clHttpRequest1.Header.ExtraFields.Add('custom-header: a value follows here');

Sergey Shirokov (November 15, 2019 at 12:07 PM)
How can I find the BuildJSONRequest?? Can you help me?
MATHEUS LEANDRO FERREIRA (November 30, 2020 at 5:24 PM)
This method is declared within the TclHttpRequest component members. Put this component on to the form and follow the example.
Sergey Shirokov (April 29, 2021 at 9:01 PM)

Add Feedback