Page 1 of 1

TBytes array in post request

Posted: Tue Feb 11, 2020 9:22 am
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.

Re: TBytes array in post request

Posted: Tue Feb 11, 2020 9:47 am
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.

Re: TBytes array in post request

Posted: Wed Feb 12, 2020 1:52 pm
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 :)

Re: TBytes array in post request

Posted: Thu Feb 13, 2020 8:48 am
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.

Re: TBytes array in post request

Posted: Thu Feb 13, 2020 12:22 pm
by igor666
Thank you very much!