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);
 
    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';

Article ID: 78, Created On: 8/27/2015, Modified: 8/27/2015