#1 2024-02-10 07:49:03

bigheart
Member
Registered: 2014-08-01
Posts: 45

THttpClientSocket.Post using THttpMultiPartStream

Hello,

I just try to communicate with telegram bot api sendmessage using THttpClientSocket.

  lHttp := THttpClientSocket.Create;
  LParam := THttpMultiPartStream.Create;

  lURL_TELEG := 'https://api.telegram.org/bot' + FToken + '/sendmessage';
  LUri.From(lURL_TELEG);
  lHttp.Open(LUri.Server, LUri.Port, nlTcp, 10000, LUri.Https);

  LParam.AddContent('chat_id', chat_id);
  LParam.AddContent('text', text);

  lHttpResponse := lHttp.Post(LUri.Address, LParam, 'multipart/formdata');

And I received the response error 400 which is "Bad Request: message text is empty".
But I checked that text = 'sample text' not empty.

Am i do right usage THttpClientSocket and THttpMultiPartStream?

Offline

#2 2024-02-10 09:06:00

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,303
Website

Re: THttpClientSocket.Post using THttpMultiPartStream

Did you call the LParam.Flush method as properly documented?

Offline

#3 2024-02-11 00:17:29

bigheart
Member
Registered: 2014-08-01
Posts: 45

Re: THttpClientSocket.Post using THttpMultiPartStream

...
LParam.AddContent('text', text);
LParam.Flush;
lHttpResponse := lHttp.Post(LUri.Address, LParam, 'multipart/formdata');
...

The result is same, error code 400.

And i tried to use sendphoto method of the telegram bot API as blow:

...
LParam.AddContent('chat_id', FloatToStr(ChatID));
LParam.AddContent('bmp', 'image/bmp');
LParam.AddFileContent('photo', LPath, LFile);
//    LParam.AddFile('photo', LPath);
LParam.Flush();
lHttpResponse := lHttp.Post(LUri.Address, LParam, 'multipart/formdata');
...

And i received the result error code 400 : "Bad Request: there is no photo in the request"

I think, I don't seem to know how to use THttpMultiPartStream and THttpClientSocket.Post function.

Please give me some advice.

Last edited by bigheart (2024-02-11 04:29:39)

Offline

#4 2024-02-11 14:38:29

tbo
Member
Registered: 2015-04-20
Posts: 337

Re: THttpClientSocket.Post using THttpMultiPartStream

bigheart wrote:

I think, I don't seem to know how to use THttpMultiPartStream and THttpClientSocket.Post function.

Please give me some advice.

Did you watch the stream before sending it? The following simple test looks ok to me:

var
  mpStream: THttpMultiPartStream;
  fileStream: THandleStream;
begin
  mpStream := THttpMultiPartStream.Create;
  try
    mpStream.AddContent('Field01', '100', TEXT_CONTENT_TYPE);
    mpStream.AddContent('Field02', '{"Name": "Thomas"}', JSON_CONTENT_TYPE);
    mpStream.AddFileContent('Data', 'data.json', '{"Name": "Thomas","Nickname": "tbo"}', JSON_CONTENT_TYPE, 'binary');
    mpStream.AddFile('Test', MakePath([Executable.ProgramFilePath, 'test.json']));
    mpStream.AddFile('Image', MakePath([Executable.ProgramFilePath, 'image.png']));
    fileStream := TFileStreamEx.Create(MakePath([Executable.ProgramFilePath, 'mpTest.dat']), fmCreate);
    try
      mpStream.Flush;
      StreamCopyUntilEnd(mpStream, fileStream);
    finally
      fileStream.Free;
    end;
  finally
    mpStream.Free;
  end;

With best regards
Thomas

Offline

#5 2024-02-12 03:35:54

Chaa
Member
Registered: 2011-03-26
Posts: 245

Re: THttpClientSocket.Post using THttpMultiPartStream

This code works for me:

function SendTgDoc(const Token, Chat, Text: RawUtf8;
    const Doc: RawByteString): boolean;
var
    LMultiPart: THttpMultiPartStream;
    LContentType, LData: RawUtf8;
    LClient: THttpClientSocket;
begin
    LMultiPart := THttpMultiPartStream.Create();
    try
        LMultiPart.AddContent('chat_id', Chat, 'text/plain');
        LMultiPart.AddContent('caption', Text, 'text/plain');
        LMultiPart.AddFileContent('document', 'attach.pdf', Doc, 'application/pdf', 'binary');
        LMultiPart.Flush();
        LClient := THttpClientSocket.Open('api.telegram.org', '443', nlTcp, 5000, True);
        try
            LClient.Post('/bot' + Token + '/sendDocument',
                LMultiPart, LMultiPart.MultipartContentType);
        finally
            LClient.Free();
        end;
    finally
        LMultiPart.Free();
    end;
end;

Offline

#6 2024-02-12 05:27:00

bigheart
Member
Registered: 2014-08-01
Posts: 45

Re: THttpClientSocket.Post using THttpMultiPartStream

Thank you chaa.

It works on your code.

Offline

Board footer

Powered by FluxBB