Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a example to take response body from request network? #156

Open
goni098 opened this issue Jul 3, 2023 · 1 comment
Open

Is there a example to take response body from request network? #156

goni098 opened this issue Jul 3, 2023 · 1 comment

Comments

@goni098
Copy link

goni098 commented Jul 3, 2023

No description provided.

@janvotava
Copy link

janvotava commented Dec 18, 2024

I'm doing something like this:

use chromiumoxide::{
    browser::{Browser, BrowserConfig},
    cdp::browser_protocol::fetch::{
        self, ContinueRequestParams, EventRequestPaused, GetResponseBodyParams, RequestPattern,
        RequestStage,
    },
};
use futures::StreamExt;
use std::sync::Arc;
use tracing::{error, warn};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    tracing_subscriber::fmt::init();

    let (mut browser, mut handler) = Browser::launch(
        BrowserConfig::builder()
            .with_head()
            .window_size(1280, 720)
            .viewport(None)
            .build()?,
    )
    .await?;

    let browse_handle = tokio::task::spawn(async move {
        while let Some(h) = handler.next().await {
            if h.is_err() {
                break;
            }
        }
    });

    let page = browser.new_page("about:blank").await?;
    page.execute(
        fetch::EnableParams::builder()
            .pattern(
                RequestPattern::builder()
                    .url_pattern("*")
                    .request_stage(RequestStage::Response)
                    .build(),
            )
            .build(),
    )
    .await?;

    let page = Arc::new(page);
    let intercept_page = page.clone();

    let mut request_paused = page.event_listener::<EventRequestPaused>().await?;
    let intercept_handle = tokio::task::spawn(async move {
        while let Some(event) = request_paused.next().await {
            let res = intercept_page
                .execute(GetResponseBodyParams::new(event.request_id.clone()))
                .await;

            match res {
                Ok(res) => {
                    dbg!(&res.body);
                }
                Err(e) => {
                    error!("Failed to get response body: {e}");
                }
            }

            if let Err(e) = intercept_page
                .execute(ContinueRequestParams::new(event.request_id.clone()))
                .await
            {
                warn!("Failed to continue request: {e}");
            }
        }
    });

    page.goto("https://www.wikipedia.org/").await?;
    page.find_element("input#searchInput")
        .await?
        .click()
        .await?
        .type_str("Rust programming language")
        .await?
        .press_key("Enter")
        .await?;

    let _html = page.wait_for_navigation().await?.content().await?;

    browser.close().await?;
    browse_handle.await?;
    intercept_handle.await?;

    Ok(())
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants