Page 1 of 1

How to capture the entire response body from a specified URL?

Posted: Mon Feb 03, 2025 9:15 am
by juansaja
I have code like this, but it doesn't work perfectly for JSON and others. It only works perfectly for text/html.

Code: Select all

procedure TB0C8179BCB4B.Chromium1ResourceLoadComplete(Sender: TObject; //
const browser: ICefBrowser; //
const frame: ICefFrame; //
const request: ICefRequest; //
const response: ICefResponse; //
status: TCefUrlRequestStatus; //
receivedContentLength: Int64 //
  );
begin
if (frame <> nil) and frame.IsValid then
  begin
  if response.MimeType = 'font/woff2' then
    begin
    end
  else if response.MimeType = 'image/gif' then
    begin
    end
  else if response.MimeType = 'image/png' then
    begin
    end
  else if response.MimeType = 'text/xml' then
    begin
    end
  else if response.MimeType = 'text/plain' then
    begin
    end
  else if response.MimeType = 'text/html' then
    begin
    // va/or := nil;
    end
  else if response.MimeType = 'application/javascript' then
    begin
     var
     visitor := TCustomStringVisitor.Create(
     procedure(A804D4E5D852: string)
     begin
     TThread.Synchronize(nil,
     procedure
     begin
     CF04D6773514.Lines.Insert(0, //
     '===========Start===============' //
     + #13#10 + 'MimeType: ' + response.MimeType //
     + #13#10 + 'Size: ' + FormatFloat(',0', receivedContentLength) //
     + #13#10 + A804D4E5D852 //
     + #13#10 + '===========Finish===============');
     end);
     end);
     frame.GetSource(visitor);
     visitor := nil;
    end
  else if response.MimeType = 'application/json' then
    begin
    var
    visitor := TCustomStringVisitor.Create(
      procedure(A804D4E5D852: string)
      begin
      TThread.Synchronize(nil,
        procedure
        begin
        CF04D6773514.Lines.Insert(0, //
          '===========Start===============' //
          + #13#10 + 'MimeType: ' + response.MimeType //
          + #13#10 + 'Size: ' + FormatFloat(',0', receivedContentLength) //
          + #13#10 + A804D4E5D852 //
          + #13#10 + '===========Finish===============');
        end);
      end);
    frame.GetSource(visitor);
    visitor := nil;
    end
  else
    begin
    var
    visitor := TCustomStringVisitor.Create(
      procedure(A804D4E5D852: string)
      begin
      TThread.Synchronize(nil,
        procedure
        begin
        CF04D6773514.Lines.Insert(0, //
          '===========Start===============' //
          + #13 + 'MimeType: ' + response.MimeType //
          + #13 + 'Size: ' + FormatFloat(',0', receivedContentLength) //
          + #13 + A804D4E5D852 //
          + #13 + '===========Finish===============');
        end);
      end);
    frame.GetSource(visitor);
    visitor := nil;
    end;
  end;
end;
Here are the type declaration and the body.

Code: Select all

  TCustomStringVisitor = class(TCefStringVisitorOwn)
  private
    FCallback: TProc<string>;


  protected
    procedure Visit(const str: ustring); override;


  public
    constructor Create(const callback: TProc<string>); reintroduce;
  end;

constructor TCustomStringVisitor.Create(const callback: TProc<string>);
begin
inherited Create;
FCallback := callback;
end;

procedure TCustomStringVisitor.Visit(const str: ustring);
begin
if Assigned(FCallback) then
  FCallback(str);
end;

Re: How to capture the entire response body from a specified URL?

Posted: Tue Feb 04, 2025 2:41 pm
by salvadordf
Hi,

ICefFrame.GetSource is used to retrieve the HTML source of that frame as a string sent to the specified visitor.
ICefFrame.GetText is used to retrieve the display text of that frame as a string sent to the specified visitor.

If you have the URL of any other resource type try downloading it with TChromiumCore.StartDownload or using the code in the URLRequest demo.
If that's not possible then consider using the ResponseFilterBrowser demo.