TBytes array in post request

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

TBytes array in post request

Post by igor666 » Tue Feb 11, 2020 9:22 am

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: 2608
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: TBytes array in post request

Post by salvadordf » Tue Feb 11, 2020 9:47 am

Hi,

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

The code should be very similar to this :
https://github.com/salvadordf/CEF4Delphi/blob/a975f8ff89de2a862c0b5eda5127268e0c86dca2/demos/Delphi_VCL/URLRequest/uURLRequest.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: 34
Joined: Fri Feb 08, 2019 1:25 pm

Re: TBytes array in post request

Post by igor666 » Wed Feb 12, 2020 1:52 pm

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: 2608
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: TBytes array in post request

Post by salvadordf » Thu Feb 13, 2020 8:48 am

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: 34
Joined: Fri Feb 08, 2019 1:25 pm

Re: TBytes array in post request

Post by igor666 » Thu Feb 13, 2020 12:22 pm

Thank you very much!

Post Reply