To send content using the POST method in Delphi, you can use the TIdHTTP component from the Indy library, which is a popular library for internet components in Delphi. Below is an example Delphi code snippet demonstrating how to send content using the POST method: Below code is the example unit MainForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdHTTP, StdCtrls;
type
TForm1 = class(TForm)
IdHTTP1: TIdHTTP;
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
Params: TStringList;
ResponseContent: string;
begin
// Create a TStringList to hold the parameters you want to send
Params := TStringList.Create;
try
// Add parameters to the list (replace 'key' and 'value' with your actual data)
Params.Add('key1=value1');
Params.Add('key2=value2');
// Use the TIdHTTP component to send a POST request with the parameters
ResponseContent := IdHTTP1.Post('https://example.com/api', Params);
// Display the response content in the Memo component
Memo1.Lines.Text := ResponseContent;
finally
Params.Free;
end;
end;
end.
In case that you need URL Encoding, below code will be helpful: unit MainForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdURI, StdCtrls;
...
procedure TForm1.Button1Click(Sender: TObject);
var
OriginalText, EncodedText: string;
begin
// Get the original text from the Edit component
OriginalText := Edit1.Text;
// Use the URLEncode function to encode the text
EncodedText := TIdURI.URLEncode(OriginalText);
// Display the encoded text in the Memo component
Memo1.Lines.Text := 'Original Text: ' + OriginalText + sLineBreak +
'Encoded Text: ' + EncodedText;
end;
end.
Tags: IdURI POST POST Method TIdHTTP URLEncode
|