1 个不稳定版本

0.1.0 2019年11月24日

#406HTTP客户端

Download history 34/week @ 2024-03-13 51/week @ 2024-03-20 71/week @ 2024-03-27 54/week @ 2024-04-03 42/week @ 2024-04-10 40/week @ 2024-04-17 37/week @ 2024-04-24 36/week @ 2024-05-01 39/week @ 2024-05-08 47/week @ 2024-05-15 56/week @ 2024-05-22 62/week @ 2024-05-29 44/week @ 2024-06-05 44/week @ 2024-06-12 48/week @ 2024-06-19 31/week @ 2024-06-26

每月175次下载

MIT 许可协议

150KB
3.5K SLoC

rttp_client

rttp_client

附加功能

rttp_client 是一个最小的HTTP客户端,默认功能仅支持HTTP请求,但您可以添加功能以支持HTTPS请求和异步支持

名称 注释
异步 异步请求功能
tls-native 使用 native-tls crate 支持HTTPS请求
tls-rustls 使用 rustls crate 支持HTTPS请求

默认使用

[dependencies]
rttp_client = "0.1"

使用 tls-native

[dependencies]
rttp_client = { version = "0.1", features = ["tls-native"] }

使用 tls-rustls

[dependencies]
rttp_client = { version = "0.1", features = ["tls-rustls"] }

异步支持

[dependencies]
rttp_client = { version = "0.1", features = ["async"] }

完全支持

[dependencies]
rttp_client = { version = "0.1", features = ["async", "tls-native"] }

重要 tls-nativetls-rustls 仅支持在功能上选择,不要同时使用。

示例

GET

# use rttp_client::HttpClient;
HttpClient::new().get()
  .url("http://httpbin.org/get")
  .emit();

POST

# use rttp_client::HttpClient;
HttpClient::new().post()
  .url("http://httpbin.org/post")
  .emit();

头部

# use rttp_client::HttpClient;
# use rttp_client::types::Header;
# use std::collections::HashMap;
let mut multi_headers = HashMap::new();
multi_headers.insert("name", "value");
HttpClient::new().get()
 .url("http://httpbin.org/get")
 .header("name=value&name=value")
 .header(("name", "value", "name=value&name=value"))
 .header(Header::new("name", "value"))
 .header(multi_headers)
 .emit();

参数

# use rttp_client::HttpClient;
# use rttp_client::types::Para;
# use std::collections::HashMap;
let mut multi_para = HashMap::new();
multi_para.insert("name", "value");
HttpClient::new().post()
  .url("http://httpbin.org/post")
  .para("name=value&name=value")
  .para(("name", "value", "name=value&name=value"))
  .para(Para::new("name", "value"))
  .para(multi_para)
  .emit();

URL

# use rttp_client::HttpClient;
# use rttp_client::types::RoUrl;
HttpClient::new().get()
  .url(RoUrl::with("http://httpbin.org").path("get").para("name=value").para(("from", "rttp")))
  .emit();

POST JSON

# use rttp_client::HttpClient;
HttpClient::new().post()
  .url("http://httpbin.org/post")
  .content_type("application/json")
  .raw(r#" {"id": 1, "from": "rttp"} "#)
  .emit();

表单和上传文件

# use rttp_client::HttpClient;
# use rttp_client::types::FormData;
# use std::collections::HashMap;
let mut multi_form = HashMap::new();
multi_form.insert("name", "value");
HttpClient::new().post()
  .url("http://httpbin.org/post")
  .para("name=value")
  .form("name=value")
  .form("name=value&name=value")
  .form(("name", "value", "name=value&name=value"))
  .form("file=@filename#/path/to/file")
  .form("file=@/path/to/file")
  .form(multi_form)
  .form(FormData::with_text("name", "value"))
  .form(FormData::with_file("name", "/path/to/file"))
  .form(FormData::with_file_and_name("name", "/path/to/file", "filename"))
  .form(FormData::with_binary("name", vec![]))  // Vec<u8>
  .emit();

参数和表单可以混合使用,参数不支持文件解析

代理

BASIC

# use rttp_client::HttpClient;
# use rttp_client::types::Proxy;
HttpClient::new().post()
  .url("http://httpbin.org/post")
  .content_type("application/json")
  .raw(r#" {"id": 1, "from": "rttp"} "#)
  .proxy(Proxy::http("127.0.0.1", 1081))
  .emit();

带授权的BASIC

# use rttp_client::HttpClient;
# use rttp_client::types::Proxy;
HttpClient::new().post()
  .url("http://httpbin.org/post")
  .content_type("application/json")
  .raw(r#" {"id": 1, "from": "rttp"} "#)
  .proxy(Proxy::socks5_with_authorization("127.0.0.1", 1081, "username", "password"))
  .emit();

自动重定向

# use rttp_client::HttpClient;
# use rttp_client::Config;
let response = HttpClient::new().post()
  .config(Config::builder().auto_redirect(true))
  .get()
  .url("http://bing.com")
  .emit();
assert!(response.is_ok());
let response = response.unwrap();
assert_ne!("bing.com", response.host());

异步

# use rttp_client::HttpClient;
# #[cfg(feature = "async")]
let response = HttpClient::new().post()
  .url("http://httpbin.org/post")
  .rasync()
  .await;

依赖项

~2–18MB
~247K SLoC