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.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

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

Post Reply
juansaja
Posts: 1
Joined: Mon Feb 03, 2025 9:10 am

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

Post 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;
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

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

Post 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.
Post Reply