If you are using Indy, the code would look something like this:
uses
..., System.Classes, IdCoderMIME, IdHTTP, System.IOUtils;
procedure Main(const filename, MassegeTxt, mobileNo: String);
var
WebRequest: TIdHTTP;
SSL: TIdSSLIOHandlerSocketOpenSSL;
bytes: TBytes;
file: String;
instance_id: String;
token: String;
mobile_number: String;
postdata: TStringList;
begin
// send image as base64
bytes := TFile.ReadAllBytes(filename);
file := TIdEncoderMIME.EncodeBytes(bytes);
instance_id := 'instance xxxxx';
token := 'xxxxxxx';
mobile_number := mobileNo;
WebRequest := TIdHTTP.Create;
try
SSL := TIdSSLIOHandlerSocketOpenSSL.Create(WebRequest);
SSL.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
WebRequest.IOHandler := SSL;
WebRequest.Request.ContentType := 'application/x-www-form-urlencoded';
postdata := TStringList.Create;
try
postdata.Add('token=' + token);
postdata.Add('to=' + mobileNo);
postdata.Add('image=' + file);
postdata.Add('caption=' + MassegeTxt);
Writeln(WebRequest.Post('https://api.ultramsg.com/' + instance_id + '/messages/image', postdata));
finally
postdata.Free;
end;
finally
WebRequest.Free;
end;
end;
If you want to use Embarcadero's TNetHTTPClient, the code would look something like this instead:
uses
..., System.Classes, System.SysUtils, System.NetEncoding, System.Net.HTTPClientComponent, System.IOUtils;
procedure Main(const filename, MassegeTxt, mobileNo: String);
var
WebClient: TNetHTTPClient;
bytes: TBytes;
file: String;
instance_id: String;
token: String;
mobile_number: String;
postdata: TStringList;
begin
// send image as base64
bytes := TFile.ReadAllBytes(filename);
file := TNetEncoding.Base64.EncodeBytesToString(bytes);
instance_id := 'instance xxxxx';
token := 'xxxxxxx';
mobile_number := mobileNo;
WebClient := TNetHTTPClient.Create(nil);
try
WebClient.ContentType := 'application/x-www-form-urlencoded';
postdata := TStringList.Create;
try
postdata.Add('token=' + token);
postdata.Add('to=' + mobileNo);
postdata.Add('image=' + file);
postdata.Add('caption=' + MassegeTxt);
Writeln(WebClient.Post('https://api.ultramsg.com/' + instance_id + '/messages/image', postdata, nil, TEncoding.UTF8, nil).ContentAsString());
finally
postdata.Free;
end;
finally
WebClient.Free;
end;
end;
Or, using Embarcadero's THTTPClient instead:
uses
..., System.Classes, System.SysUtils, System.IOUtils, System.NetEncoding, System.Net.HttpClient;
procedure Main(const filename, MassegeTxt, mobileNo: String);
var
WebClient: THTTPClient;
bytes: TBytes;
file: String;
instance_id: String;
token: String;
mobile_number: String;
postdata: TStringList;
begin
// send image as base64
bytes := TFile.ReadAllBytes(filename);
file := TNetEncoding.Base64.EncodeBytesToString(bytes);
instance_id := 'instance xxxxx';
token := 'xxxxxxx';
mobile_number := mobileNo;
WebClient := THTTPClient.Create;
try
WebClient.ContentType := 'application/x-www-form-urlencoded';
postdata := TStringList.Create;
try
postdata.Add('token=' + token);
postdata.Add('to=' + mobileNo);
postdata.Add('image=' + file);
postdata.Add('caption=' + MassegeTxt);
Writeln(WebClient.Post('https://api.ultramsg.com/' + instance_id + '/messages/image', postdata, nil, TEncoding.UTF8, nil).ContentAsString());
finally
postdata.Free;
end;
finally
WebClient.Free;
end;
end;