How to send a GET request with JSON data

 
 
There are three different ways to send JSON requests and receiving responses:
 
1. Sending a JSON string and receiving a non-parsed JSON response. This code uses a pre-composed request string in JSON format for generating an HTTP request. This approach requires a correctly formatted and encoded data in JSON format.
 
procedure TForm1.btnSendJSonStringClick(Sender: TObject);
var
  request: string;
  response: TStringStream;
begin
  request :=
'{' +
' "client_ref": "ABC123456",' +
' "customer_title": "Mr",' +
' "customer_firstname": "John",' +
' "customer_surname": "Smith",' +
' "customer_address": "123 The Lane, Sunnyvale",' +
' "customer_postcode": "ZZ1 1AA",' +
' "customer_telephone": "07711911911",' +
' "faults_client_description": "Issue with PROBLEM MIDDLE STITCHING IS MISSING (DIMPLE)",' +
' "clients_client": "Tony''s furniture shop",' +
' "furniture_manufacturer": "Manufacturer Name",' +
' "furniture_model": "Model Name",' +
' "furniture_batch": "12334",' +
' "furniture_colour": "Red",' +
' "furniture_delivery_date": "2019-07-08"' +
'}';

  response := TStringStream.Create();
  try
    clHttpRequest1.BuildJSONRequest(request);
    clHttp1.SendRequest('GET', 'https://dev2.system.ism.furniture/apiv2/jobs/add', clHttpRequest1, response);
  finally
    response.Free();
  end;
end;
2. Using the TclJSONObject class for specifying JSON properties. This approach doesn't require class definitions for requests and responses. You can add necessary request properties using the TclJSONObject.AddString, AddValue, or AddBoolean methods. Also, you can add a property of Object type using the AddMember method. Server responses can be transformed into TclJSONObject using the TclJSONObject.ParseObject method.
 
procedure TForm1.btnSendJSonObjectClick(Sender: TObject);
var
  request: TclJSONObject;
  response: TclJSONObject;
  responseBody: TStringStream;
begin
  request := nil;
  response := nil;
  responseBody := nil;
  try
    request := TclJSONObject.Create();

    request.AddString('client_ref', 'ABC123456');
    request.AddString('customer_title', 'Mr');
    request.AddString('customer_firstname', 'John');
    request.AddString('customer_surname', 'Smith');
    request.AddString('customer_address', '123 The Lane,'#13#10' Sunnyvale');
    request.AddString('customer_postcode', 'ZZ1 1AA');
    request.AddString('customer_telephone', '07711911911');
    request.AddString('faults_client_description', 'Issue with PROBLEM MIDDLE STITCHING IS MISSING (DIMPLE)');
    request.AddString('clients_client', 'Tony''s furniture shop');
    request.AddString('furniture_manufacturer', 'Manufacturer Name');
    request.AddString('furniture_model', 'Model Name');
    request.AddString('furniture_batch', '12334');
    request.AddString('furniture_colour', 'Red');
    request.AddString('furniture_delivery_date', '2019-07-08');
    request.AddString('general_instructions', 'Ring customer ASAP');

    clHttpRequest1.BuildJSONRequest(request);

    responseBody := TStringStream.Create();

    clHttp1.SendRequest('GET', 'https://dev2.system.ism.furniture/apiv2/jobs/add', clHttpRequest1, responseBody);

    response := TclJSONObject.ParseObject(responseBody.DataString);
  finally
    responseBody.Free();
    response.Free();
    request.Free();
  end;
end;
3. Using business objects for JSON requests and responses. This way requires defining special classes for both requests and responses (see both the TCreateJobServiceRequest and the TCreateJobServiceResponse classes in the SendJSonGetRequest program). This code utilizes special Delphi property attributes for providing JSonSerializer with property names and its' types. After that, you can serialize a JSON request and send it to a server. JSON responses can be de-serialized to objects, as well. This approach requires Delphi XE3 and higher.  If you modify the sources of the JSonSerializer library and remove all references to the RAD Studio namespaces within the 'uses' sections, you can use the library in Delphi 2009, 2010, XE, and XE2, as well.
 
procedure TForm1.btnSendSerializedObjectClick(Sender: TObject);
var
  request: TCreateJobServiceRequest;
  requestBody: string;
  response: TCreateJobServiceResponse;
  responseBody: TStringStream;
  serializer: TclJsonSerializer;
begin
  request := nil;
  response := nil;
  responseBody := nil;
  serializer := nil;
  try
    request := TCreateJobServiceRequest.Create();

    request.ClientRef := 'ABC123456';
    request.CustomerTitle := 'Mr';
    request.CustomerFirstname := 'John';
    request.CustomerSurname := 'Smith';
    request.CustomerAddress := '123 The Lane,'#13#10' Sunnyvale';
    request.CustomerPostcode := 'ZZ1 1AA';
    request.CustomerTelephone := '07711911911';
    request.FaultsClientDescription := 'Issue with PROBLEM MIDDLE STITCHING IS MISSING (DIMPLE)';
    request.ClientsClient := 'Tony''s furniture shop';
    request.FurnitureManufacturer := 'Manufacturer Name';
    request.FurnitureModel := 'Model Name';
    request.FurnitureBatch := '12334';
    request.FurnitureColour := 'Red';
    request.FurnitureDeliveryDate := '2019-07-08';
    request.GeneralInstructions := 'Ring customer ASAP';

    serializer := TclJsonSerializer.Create();

    requestBody := serializer.ObjectToJson(request);

    clHttpRequest1.BuildJSONRequest(requestBody);

    responseBody := TStringStream.Create();

    clHttp1.SendRequest('GET', 'https://dev2.system.ism.furniture/apiv2/jobs/add', clHttpRequest1, responseBody);

    response := serializer.JsonToObject(TCreateJobServiceResponse, responseBody.DataString) as TCreateJobServiceResponse;
  finally
    serializer.Free();
    responseBody.Free();
    response.Free();
    request.Free();
  end;
end;
Which approach to choose? The answer mostly depends on your project requirements. The number one is simple, but you need to encode JSON data manually. All JSON members are used in text format. So Delphi compiler cannot validate them. The same for the number two. This approach allows you to automatically encode/decode JSON data. But you need to validate JSON members manually. The number three requires definitions for all requests and responses. If you have a business model or a data contract for your REST service, this is preferable way of exchanging data in JSON format. This approach is used in our Google Calendar API, Dropbox API, and JSON Serialization articles:
 
 

Add Feedback