request-http

Crates.iorequest-http
lib.rsrequest-http
version0.1.1
sourcesrc
created_at2023-11-21 02:40:04.631346
updated_at2023-11-21 02:59:38.977181
descriptionSend http or https request.
homepagehttps://github.com/poohlaha/rust-tools/tree/main/packages/http
repositoryhttps://github.com/poohlaha/rust-tools/tree/main/packages/http
max_upload_size
id1043660
size43,142
Kauren J (poohlaha)

documentation

README

request-http

send a http or https request, include form-data, form submit.

Usage

To use http, first add this to your Cargo.toml:

[dependencies]
request-http = "1.0"

Next, add this to your crate:

use request_http::{client_send, client_send_form_data, download};

fn main() {
    // ...
}

Examples

Create an HTTP get request:

use request_http::client_send;

#[tokio::main]
async fn main() {
    let url = String::from("https://www.rust-lang.org/");
    let options = Options {
        url,
        data: None,
        form: None,
        method: Some("get".to_string()),
        headers: None,
        timeout: None,
    };
    let response: HttpResponse = client_send(options, false).await?;
}

Create an HTTP post request:

use request_http::client_send;

#[tokio::main]
async fn main() {
    let url = String::from("https://www.rust-lang.org/");
    let data = serde_json::json!({
          "data": {
            "test":"123456"
          },
          "requestTime": "202306171000",
          "version": "1.0"
        });

    let options = Options {
        url,
        data: Some(data),
        form: None,
        method: None,
        headers: None,
        timeout: None,
    };
    let response: HttpResponse = client_send(options, false).await?;
}

Create an HTTP form-data request:

use request_http::{client_send_form_data, HttpFormData};

#[tokio::main]
async fn main() {
    let url = String::from("https://example.com/api/upload");
    let form = HttpFormData::new()
        .text("userId", "10074")
        .text("version", "1.0")
        .file("files", "/usr/local/text.zip")
        .unwrap();

    let options = Options {
        url,
        data: None,
        form: Some(form),
        method: None,
        headers: None,
        timeout: None,
    };
    let response: HttpResponse = client_send_form_data(options)?;
}

Download file with progress bar:

use request_http::download;

#[tokio::main]
async fn main() {
    download(
        DownloadOptions {
            url: "https://example.com/api/download",
            file_name: None,
            timeout: None,
            output_dir: Some(args.workspace.clone()),
            overwrite: Some(true),
        },
        None, // if u use process bar, please create `MultiProgress`
    ).await?;
}

License

Apache License, Version 2.0 (LICENSE or https://apache.org/licenses/LICENSE-2.0)

Commit count: 0

cargo fmt