Crates.io | request-http |
lib.rs | request-http |
version | 0.1.1 |
source | src |
created_at | 2023-11-21 02:40:04.631346 |
updated_at | 2023-11-21 02:59:38.977181 |
description | Send http or https request. |
homepage | https://github.com/poohlaha/rust-tools/tree/main/packages/http |
repository | https://github.com/poohlaha/rust-tools/tree/main/packages/http |
max_upload_size | |
id | 1043660 |
size | 43,142 |
send a http
or https
request, include form-data
, form submit
.
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() {
// ...
}
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?;
}
Apache License, Version 2.0 (LICENSE or https://apache.org/licenses/LICENSE-2.0)