Jump to content
mofareh

post image base64 to whatsapp api

Recommended Posts

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 by mofareh

Share this post


Link to post
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
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

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
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 by mofareh

Share this post


Link to post

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
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

 

 

yes.png

Edited by mofareh

Share this post


Link to post
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 

 

   

no.jpg

yes.png

Edited by mofareh

Share this post


Link to post

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
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 by mofareh

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×