mofareh 2 Posted September 12, 2022 (edited) hello there this code for send image to whatsapp api i have this code but the parameters post with QueryString ... i want to post parameters with data forms var client := TRESTClient.Create('https://api.ultramsg.com/instance16802/messages/image'); var request := TRESTRequest.Create(nil); var esponse := TRESTResponse.Create(nil); request.Client := client; request.Response := esponse; client.ContentType:= 'application/x-www-form-urlencoded'; request.Method := TRESTRequestMethod.rmPOST; request.AddParameter('token','xxxxxx', TRESTRequestParameterKind.pkGETorPOST); request.AddParameter('to','+0000000', TRESTRequestParameterKind.pkGETorPOST); request.AddParameter('image',file2, TRESTRequestParameterKind.pkGETorPOST); //file2 as string : converted image to base64 request.Execute; Memo1.Text := request.Response.Content; Edited September 12, 2022 by mofareh Share this post Link to post
Dave Nottage 557 Posted September 12, 2022 1 hour ago, mofareh said: i have this code but the parameters post with QueryString ... i want to post parameters with data forms Is it failing? If so, what's the response? Also, please show how you are encoding the image to base64. If using TNetEncoding.Base64, you may encounter problems because it inserts line breaks by default, which may not be what the API is expecting. Share this post Link to post
mofareh 2 Posted September 12, 2022 13 minutes ago, Dave Nottage said: Is it failing? If so, what's the response? Also, please show how you are encoding the image to base64. If using TNetEncoding.Base64, you may encounter problems because it inserts line breaks by default, which may not be what the API is expecting. thank you for replay i find the way below when i change parameter to TJSONObject it is done : procedure TForm6.Button8Click(Sender: TObject); var byte1: TBytes; file2:widestring; LJSONObject: TJSONObject; begin if OpenDialog1.Execute then try //// begin encodein base64 byte1:=System.iOUtils.TFile.ReadAllBytes(OpenDialog1.FileName); file2:= TNetEncoding.Base64.EncodeBytesToString(byte1); file2:=trim(StringReplace(StringReplace(file2, #10, '', [rfReplaceAll]), #13, '',[rfReplaceAll])); //// end encodein base64 var client := TRESTClient.Create('https://api.ultramsg.com/instance16802/messages/image'); var request := TRESTRequest.Create(nil); var esponse := TRESTResponse.Create(nil); request.Client := client; request.Response := esponse; client.ContentType:= 'application/x-www-form-urlencoded'; request.Method := TRESTRequestMethod.rmPOST; LJSONObject := TJSONObject.Create(); LJSONObject.AddPair('token','xxxxxx'); LJSONObject.AddPair('to','+00000000'); LJSONObject.AddPair('image', file2); request.AddBody(LJSONObject); request.Execute; Memo1.Text := request.Response.Content; finally LJSONObject.Free; end; end; Share this post Link to post
Lajos Juhász 293 Posted September 12, 2022 Why is file2 a widestring? You should try: procedure TForm1.Button1Click(Sender: TObject); var byte1: TBytes; file2: string; LJSONObject: TJSONObject; base64: TBase64Encoding; begin if OpenDialog1.Execute then begin /// / begin encodein base64 byte1 := System.iOUtils.TFile.ReadAllBytes(OpenDialog1.FileName); base64 := TBase64Encoding.Create(0); // CharsPerLine = 0 means no line breaks try file2 := TNetEncoding.base64.EncodeBytesToString(byte1); finally base64.Free; end; var Client := TRESTClient.Create('https://api.ultramsg.com/instance16802/messages/image'); var request := TRESTRequest.Create(nil); var esponse := TRESTResponse.Create(nil); request.Client := Client; request.Response := esponse; Client.ContentType := 'application/x-www-form-urlencoded'; request.Method := TRESTRequestMethod.rmPOST; LJSONObject := TJSONObject.Create(); try LJSONObject.AddPair('token', 'xxxxxx'); LJSONObject.AddPair('to', '+00000000'); LJSONObject.AddPair('image', file2); request.AddBody(LJSONObject); request.Execute; Memo1.Text := request.Response.Content; finally LJSONObject.Free; end; end; end; Share this post Link to post
mofareh 2 Posted September 12, 2022 (edited) 13 minutes ago, Lajos Juhász said: it is not done you most be add : file2:=trim(StringReplace(StringReplace(file2, #10, '', [rfReplaceAll]), #13, '',[rfReplaceAll])); to remov #13#10 from string whene i add it , it is done Edited September 12, 2022 by mofareh Share this post Link to post
Lajos Juhász 293 Posted September 12, 2022 // CharsPerLine = 0 means no line breaks Share this post Link to post
mofareh 2 Posted September 12, 2022 i try it not done give me your email to sent token to you an try Share this post Link to post
mofareh 2 Posted September 12, 2022 than you for remark , the code done : procedure Tmain_frm.Button6Click(Sender: TObject); var byte1: TBytes; file2: string; LJSONObject: TJSONObject; base64: TBase64Encoding; begin if OpenDialog1.Execute then begin /// / begin encodein base64 byte1 := System.iOUtils.TFile.ReadAllBytes(OpenDialog1.FileName); base64 := TBase64Encoding.Create(0); // CharsPerLine = 0 means no line breaks try file2 := base64.EncodeBytesToString(byte1); finally base64.Free; end; var Client := TRESTClient.Create('https://api.ultramsg.com/instance16802/messages/image'); var request := TRESTRequest.Create(nil); var esponse := TRESTResponse.Create(nil); request.Client := Client; request.Response := esponse; Client.ContentType := 'application/x-www-form-urlencoded'; request.Method := TRESTRequestMethod.rmPOST; LJSONObject := TJSONObject.Create(); try LJSONObject.AddPair('token', token_txt.text); LJSONObject.AddPair('to', send_to_txt.text); LJSONObject.AddPair('image', file2); request.AddBody(LJSONObject); request.Execute; Memo1.Text := request.Response.Content; finally LJSONObject.Free; end; end; end; Share this post Link to post
mofareh 2 Posted September 12, 2022 (edited) 2 hours ago, Lajos Juhász said: // CharsPerLine = 0 means no line breaks can i sent with Form values? the code sent QuireyString i want Form values method Edited September 12, 2022 by mofareh Share this post Link to post
mofareh 2 Posted September 12, 2022 (edited) 6 hours ago, Dave Nottage said: Is it failing? If so, what's the response? Also, please show how you are encoding the image to base64. If using TNetEncoding.Base64, you may encounter problems because it inserts line breaks by default, which may not be what the API is expecting. the response is : 414 Request-URI Too Large the API supoort sayd the paramerter post with QuireyString and he want to post parameter to Form values Edited September 12, 2022 by mofareh Share this post Link to post
Lajos Juhász 293 Posted September 12, 2022 In the C# everything is a parameter (https://blog.ultramsg.com/send-whatsapp-message-by-whatsapp-api-c-sharp): procedure TForm1.Button6Click(Sender: TObject); var byte1: TBytes; file2: string; base64: TBase64Encoding; begin if OpenDialog1.Execute then begin /// / begin encodein base64 byte1 := System.iOUtils.TFile.ReadAllBytes(OpenDialog1.FileName); base64 := TBase64Encoding.Create(0); // CharsPerLine = 0 means no line breaks try file2 := base64.EncodeBytesToString(byte1); finally base64.Free; end; var Client := TRESTClient.Create('https://api.ultramsg.com/instance16802/messages/image'); var request := TRESTRequest.Create(nil); var esponse := TRESTResponse.Create(nil); request.Client := Client; request.Response := esponse; Client.ContentType := 'application/x-www-form-urlencoded'; request.Method := TRESTRequestMethod.rmpost; request.AddParameter('token', token_txt.text); request.AddParameter('to', send_to_txt.text); request.AddParameter('image', file2); // request.AddBody(LJSONObject); request.Execute; Memo1.Text := request.Response.Content; end; end; Share this post Link to post
mofareh 2 Posted September 12, 2022 (edited) 4 minutes ago, Lajos Juhász said: In the C# everything is a parameter (https://blog.ultramsg.com/send-whatsapp-message-by-whatsapp-api-c-sharp): this code by c# but i want it by delphi pascal using System; using RestSharp; using System.Threading.Tasks; namespace test { class Program { static async Task Main(string[] args) { string instanceId = "instance950"; // your instanceId string token = "yourtoken"; //instance Token string mobile = "14155552671"; var url = "https://api.ultramsg.com/" + instanceId + "/messages/image"; var client = new RestClient(url); var request = new RestRequest(url, Method.Post); request.AddHeader("content-type", "application/x-www-form-urlencoded"); request.AddParameter("token", token); request.AddParameter("to", mobile); request.AddParameter("image", "https://file-example.s3-accelerate.amazonaws.com/images/test.jpg"); request.AddParameter("caption", "caption text"); RestResponse response = await client.ExecuteAsync(request); var output = response.Content; Console.WriteLine(output); } } } Edited September 12, 2022 by mofareh Share this post Link to post
AVG 0 Posted December 16, 2022 Just remember to not use any API Key/Token on Delphi code en production. This is very easy decompilable and risky. Instead use a backend for this porpoise. You can use already pre-prepare backend like back4apps, that let you start very easily with a backend for app. Any encryption that you do to hide this code on client side (mobile) will make harder to get the API Keyy / Token, but will not be to much of a hard job for a hacker. Share this post Link to post