Skip to content

Instantly share code, notes, and snippets.

@wellington1993
Created September 24, 2024 19:01
Show Gist options
  • Save wellington1993/9ca2220be9050442b8e6e865c6e412e3 to your computer and use it in GitHub Desktop.
Save wellington1993/9ca2220be9050442b8e6e865c6e412e3 to your computer and use it in GitHub Desktop.
uses
System.SysUtils, Data.DBXJSON, IdHTTP, IdSSLOpenSSL;
procedure SendJSON(const AURL: string);
var
JSONObject, ImageObject, ThumbnailObject: TJSONObject;
IdHTTP: TIdHTTP;
SSLHandler: TIdSSLIOHandlerSocketOpenSSL;
JSONStream: TStringStream;
Response: string;
begin
// Criação do JSON
JSONObject := TJSONObject.Create;
try
JSONObject.AddPair('id', '0001');
JSONObject.AddPair('type', 'donut');
JSONObject.AddPair('name', 'Cake');
ImageObject := TJSONObject.Create;
ImageObject.AddPair('url', 'images/0001.jpg');
ImageObject.AddPair('width', TJSONNumber.Create(200));
ImageObject.AddPair('height', TJSONNumber.Create(200));
JSONObject.AddPair('image', ImageObject);
ThumbnailObject := TJSONObject.Create;
ThumbnailObject.AddPair('url', 'images/thumbnails/0001.jpg');
ThumbnailObject.AddPair('width', TJSONNumber.Create(32));
ThumbnailObject.AddPair('height', TJSONNumber.Create(32));
JSONObject.AddPair('thumbnail', ThumbnailObject);
// Envio do JSON via API com TLS
IdHTTP := TIdHTTP.Create(nil);
SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
JSONStream := TStringStream.Create(JSONObject.ToString, TEncoding.UTF8);
try
IdHTTP.IOHandler := SSLHandler;
IdHTTP.Request.ContentType := 'application/json';
IdHTTP.Request.CharSet := 'utf-8';
Response := IdHTTP.Post(AURL, JSONStream);
// Processar a resposta, se necessário
finally
JSONStream.Free;
SSLHandler.Free;
IdHTTP.Free;
end;
finally
JSONObject.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
SendJSON('https://api.exemplo.com/donuts');
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment