To make an HTTP POST request in Delphi FMX (FireMonkey), you can use the THTTPClient class from the System.Net.HttpClient unit. Here is an example of how you can use THTTPClient to make an HTTP POST request:
First, include the System.Net.HttpClient unit in your code by adding the following line at the top of your program:
1 |
uses System.Net.HttpClient; |
Declare a variable of type THTTPClient:
1 2 |
var HTTPClient: THTTPClient; |
Create an instance of THTTPClient:
1 |
HTTPClient := THTTPClient.Create; |
Set the URL of the server you want to send the request to:
1 |
HTTPClient.BaseURL := ‘http://www.example.com/api/’; |
Set the request headers, if necessary:
1 2 |
HTTPClient.ContentType := ‘application/json’; HTTPClient.CustomHeaders[‘Authorization’] := ‘Bearer 1234567890’; |
Set the request body:
1 2 3 4 5 |
var RequestBody: TStringStream; begin RequestBody := TStringStream.Create(‘{“name”:”John”,”age”:30}’, TEncoding.UTF8); end; |
Send the request:
1 2 3 4 5 6 |
var Response: IHTTPResponse; begin Response := HTTPClient.Post(‘users’, RequestBody); // Do something with the response end; |
Free the HTTP client when it is no longer needed:
1 |
HTTPClient.Free; |
This example shows how to make an HTTP POST request with a JSON payload, but you can also use other formats such as XML or form data. You can find more information about the THTTPClient class and the various options it provides in the Delphi documentation.
Leave a Reply