Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.

TBytes array in post request

Post Reply
igor666
Posts: 64
Joined: Fri Feb 08, 2019 1:25 pm

TBytes array in post request

Post by igor666 »

Good afternoon. Please tell me whether it is possible to add an array of TBytes to the POST request. The bottom line is, under certain conditions, the program takes a picture of its window, which is encoded in the webp format and as a result we get the TBytes array. This array must be transferred to the server in a specific request. I catch this request in the onBeforeResourceLoad event:

Code: Select all

var Screen: TBytes //global variable
//..
procedure onBeforeResourceLoad(...) 
begin
  //...
  if [some condition] then
  begin
    request.Method := 'POST';
    request.SetHeaderByName('Content-Type','application/x-www-form-urlencoded',true);
    if not Assigned(request.PostData) then
      request.PostData := TCefPostDataRef.New;
    request.PostData.AddElement(CreateField('url=' + ReportData.CurrentURL));
    //insert TBytes here "Screen"
  end;
end;
Can I add a Screen variable here in PostData? And can this be done without saving Screen to a file?
Thanks for the help.
User avatar
salvadordf
Posts: 4016
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: TBytes array in post request

Post by salvadordf »

Hi,

I've never done that but it should be possible.

The code should be very similar to this :
https://github.com/salvadordf/CEF4Delph ... t.pas#L256

That code belongs to the URLRequest demo but it adds POST elements to the request the same way.

I guess the TBytes should be encoded before you send them.
igor666
Posts: 64
Joined: Fri Feb 08, 2019 1:25 pm

Re: TBytes array in post request

Post by igor666 »

I apologize, but could you explain in more detail? In the examples, a string is used, the parameters are formed by one string with the substitution of the parameter name. But what about the flow, I do not quite understand. You mean you need to convert the TBytes array to a string and then add it to the post parameters?
Sorry if this is a stupid question, but something I can’t understand what to do :)
User avatar
salvadordf
Posts: 4016
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: TBytes array in post request

Post by salvadordf »

You need to know the server API details and send the data as the server wants.

For example, sometimes you may have to send the binary data in a POST request with a key-value pair. That value is usually encoded in base64.

The example in the URLRequest demo sends simple strings in key-value pairs but you can send anything conveniently encoded.

In order to encode or decode values you can use the functions available in Delphi, Indy, CEF4Delphi, etc.
Search "delphi encode image to base64" and you will find many code examples.
igor666
Posts: 64
Joined: Fri Feb 08, 2019 1:25 pm

Re: TBytes array in post request

Post by igor666 »

Thank you very much!
Post Reply