2 个版本

0.1.1 2023年11月21日
0.1.0 2023年11月21日

#7 in #send-http

MIT/Apache

23KB
361

request-http

发送 httphttps 请求,包括 form-data表单提交

用法

要使用 http,首先将以下内容添加到您的 Cargo.toml

[dependencies]
request-http = "1.0"

然后,将以下内容添加到您的 crate

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

fn main() {
    // ...
}

示例

创建一个 HTTP get 请求

use request_http::client_send;

#[tokio::main]
async fn main() {
    let url = String::from("https://rust-lang.net.cn/");
    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?;
}

创建一个 HTTP post 请求

use request_http::client_send;

#[tokio::main]
async fn main() {
    let url = String::from("https://rust-lang.net.cn/");
    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?;
}

创建一个 HTTP form-data 请求

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)?;
}

带有进度条的下载 文件

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?;
}

许可证

Apache 许可证第 2 版 (LICENSEhttps://apache.ac.cn/licenses/LICENSE-2.0)

依赖项

~5–21MB
~280K SLoC