HFQR / xitca-web

Apache License 2.0
727 stars 43 forks source link

http client request #1019

Closed kylak closed 5 months ago

kylak commented 6 months ago

Hello,

I tried to make a simple http client request, but I didn't succeeded, is it possible to make a simple http request at http://www.example.com/ ?

kylak.

fakeshadow commented 6 months ago

xitca-client is a work in progress project and has not been released yet. It can depends on any source including unreleased xitca crates. In order to utilize it you have to use cargo patch and override deps for said situation. You can reference https://doc.rust-lang.org/cargo/reference/overriding-dependencies.html for further usage of cargo.

Below is an example how to make your request work with latest main branch. But do note that the api and deps can have breaking changes at any time in the future beyond the 652e03a commit hash. If you need a stable api consider not using xitca-client for now. In general reqwest is a safe choice.

Cargo.toml

[dependencies]
xitca-client = "0.1"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }

[patch.crates-io]
xitca-client = { git = "https://github.com/HFQR/xitca-web.git", rev = "652e03a" }
xitca-http = { git = "https://github.com/HFQR/xitca-web.git", rev = "652e03a" }
xitca-io = { git = "https://github.com/HFQR/xitca-web.git", rev = "652e03a" }

main.rs

#[tokio::main]
async fn main() -> Result<(), xitca_client::error::Error> {
    let cli = xitca_client::Client::new();

    let res = cli.get("http://www.example.com/")?.send().await?;

    println!("response head: \r\n{res:?}");

    let body = res.string().await?;

    println!("response body: \r\n{body}");

    Ok(())
}
kylak commented 5 months ago

Thanks for the answer!