Joe
Joe

Reputation: 47739

Tracing redirects with Headless Chrome Developer Tools

I'm using CDP4J, though I expect this question relates directly to Google Chrome DevTools Protocol.

I want to get a list of the HTTP requests made for a webpage and response codes. So that would include the initial request in the main frame and subsequent requests, either made via 3xx redirects or JavaScript-originated navigation.

It's not clear how to do this reliably.

I have tried the following:

  1. Store io.webfolder.cdp.session.Session.getFrameId
  2. Add callback to session with addEventListener, record every event of type io.webfolder.cdp.event.Event.NetworkResponseReceived
  3. Of these, filter those whose frame ID matches.
  4. Of these filter on type io.webfolder.cdp.type.page.ResourceType.Document

I have a URL that I know returns a HTTP 303. But looking at the Events, don't see the original URL, but instead see only the final destination of the redirects. Every single NetworkResponseReceived has a status of 200.

How can I capture the chain of redirects?

Upvotes: 2

Views: 1249

Answers (2)

mrog
mrog

Reputation: 2134

I've been using the ResponseReceived event for this purpose. This seems to work to get the document URL from the event:

    if (session.getTargetId().equals(responseReceived.getFrameId()) && ResourceType.Document.equals(responseReceived.getType())) {
        String url = responseReceived.getResponse().getUrl();
        ...
    }

Upvotes: 0

Joe
Joe

Reputation: 47739

I found the answer. The io.webfolder.cdp.event.network.RequestWillBeSent event has getRedirectResponse, which contains a response if it's a redirect.

Upvotes: 2

Related Questions