Delphi code to send data to web server based on POST method

CK1820 
Created at Mar 09, 2024 08:53:34
Updated at Mar 09, 2024 08:55:19 
79   0   0   0  

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 Share on Facebook Share on X

◀ PREVIOUS
Good Delphi components
▶ NEXT
Run command with Administrator permission in Delphi on Microsoft Windows
  Comments 0
Login for comment
SIMILAR POSTS

PHP socket programming to get content with post method (created at Oct 19, 2009)

Is there URLEncode function in Javascript ? (created at Oct 11, 2007)

Retrieving all image links from an HTML document (created at Sep 17, 2007)


OTHER POSTS IN THE SAME CATEGORY

Building a Simple DNS Server in Delphi with TTL Support (created at Mar 16, 2024)

Run PowerShell Command as Administrator in Delphi (updated at Mar 09, 2024)

Run command with Administrator permission in Delphi on Microsoft Windows (updated at Mar 09, 2024)

Good Delphi components (created at Apr 03, 2016)

How to download/grap web pages or XML document at internet website in Delphi XE? (created at Jul 20, 2015)

Delphi Program can command level execution program with hidden mode (updated at Jan 15, 2024)

How To Turn On/Off Monitor with Delphi (updated at Mar 09, 2024)

How to get application version number in Delphi (updated at Mar 09, 2024)

The Delphi function to get My Videos folder (created at Apr 29, 2011)

Get Application Data Path on Delphi (updated at Mar 09, 2024)

I want to select directory(folder) not file. How can I do that ? (created at Apr 29, 2011)

Changing the Title of a Print Dialog in Delphi (created at Apr 20, 2011)

Auto Logon Programming in Delphi (created at Dec 31, 2010)

Thread Application Implementation in TThread (created at Apr 14, 2010)

Is there directory selection VCL component in Delphi ? (created at Sep 08, 2009)

How to launch application ? (created at Sep 08, 2009)

How to print external document ? (created at Sep 08, 2009)

Delphi API to get windows temporary directory (created at Sep 03, 2009)

UPDATES

Creating a Pinterest-Style Card Layout with Bootstrap and Masonry (created at Apr 24, 2024)

Mastering Excel Data Importation in PHP (updated at Apr 24, 2024)

JSON format control in PHP (updated at Apr 24, 2024)

Equal Height Blocks in Bootstrap with JavaScript (created at Apr 22, 2024)

How to convert integer to text string ? (updated at Apr 22, 2024)

Checking similarity between two strings in PHP (updated at Apr 21, 2024)

Create Blob Image in HTML based on the given Text, Width and Height in the Center of the Image without saving file (updated at Apr 21, 2024)

How do I determine the client IP type (IPv4/IPv6) in PHP (updated at Apr 16, 2024)

How do I determine the client IP type in Python - IPv4 or IPv6 (updated at Apr 13, 2024)

Getting Started with PyTorch: A Beginner's Guide to Building Your First Neural Network (updated at Apr 09, 2024)

Predicting Buyer Preferences with PyTorch: A Deep Learning Approach (updated at Apr 09, 2024)

Forecasting the Weather with PyTorch: A Beginner's Guide to Temperature Prediction (created at Apr 09, 2024)

PyTorch example to Forcast Stock Price based on 10 days Dataset (created at Apr 09, 2024)

Mastering Model Persistence: Saving and Loading Trained Machine Learning Models in Python (created at Apr 08, 2024)

Harnessing the Power of Random Forest Algorithm in Python (created at Apr 08, 2024)

Understanding and Implementing K-Nearest Neighbors (KNN) Algorithm in Python (created at Apr 08, 2024)

Forecasting with Linear Regression and KNN Regression in Python (updated at Apr 07, 2024)

What is 302 Found Redirection in HTTP 1.1? (created at Apr 04, 2024)

Mastering Random Forest Regression: A Comprehensive Guide with Python Examples (updated at Apr 01, 2024)

Python Implementation of Linear Regression (updated at Apr 01, 2024)

Mastering Supervised Machine Learning with Python: A Comprehensive Guide (created at Apr 01, 2024)

Mastering AI: A Beginner's Guide to Python Programming and Beyond (created at Apr 01, 2024)

How do I create animated background for Google Meet? (updated at Mar 28, 2024)

Building a Simple DNS Server in Delphi with TTL Support (created at Mar 16, 2024)

How to force cookies, disable php sessid in URL ? (updated at Mar 16, 2024)

Implementing a Versatile DNS Server in PHP: Handling A, AAAA, CNAME, and TXT Records (updated at Mar 16, 2024)

Implementing a Versatile DNS Server in Python: Handling A, AAAA, CNAME, and TXT Records (created at Mar 16, 2024)

Building a Basic DNS Server in PHP/Python: A Beginner's Guide (updated at Mar 15, 2024)

Dynamic DNS Made Easy: Building a Python-Based Solution (created at Mar 15, 2024)

Exploring the Depths of Data Transfer: sendfile vs. kTLS (created at Mar 15, 2024)