feat: udpate fetch-rs
This commit is contained in:
@@ -22,12 +22,12 @@ let response = fetch(
|
|||||||
"https://hatter.ink/util/print_request.action",
|
"https://hatter.ink/util/print_request.action",
|
||||||
&FetchOptions {
|
&FetchOptions {
|
||||||
method: FetchMethod::Post,
|
method: FetchMethod::Post,
|
||||||
headers: Some(vec![
|
headers: vec![
|
||||||
FetchHeader {
|
FetchHeader {
|
||||||
key: "content-type".to_string(),
|
key: "content-type".to_string(),
|
||||||
value: "application/json".to_string(),
|
value: "application/json".to_string(),
|
||||||
}
|
}
|
||||||
]),
|
],
|
||||||
body: Some("{}".as_bytes().to_vec()),
|
body: Some("{}".as_bytes().to_vec()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
})?;
|
})?;
|
||||||
|
|||||||
@@ -28,12 +28,12 @@ fn main() -> XResult<()> {
|
|||||||
"https://hatter.ink/util/print_request.action",
|
"https://hatter.ink/util/print_request.action",
|
||||||
&FetchOptions {
|
&FetchOptions {
|
||||||
method: FetchMethod::Post,
|
method: FetchMethod::Post,
|
||||||
headers: Some(vec![
|
headers: vec![
|
||||||
FetchHeader {
|
FetchHeader {
|
||||||
key: "content-type".to_string(),
|
key: "content-type".to_string(),
|
||||||
value: "application/json".to_string(),
|
value: "application/json".to_string(),
|
||||||
}
|
}
|
||||||
]),
|
],
|
||||||
body: Some("{}".as_bytes().to_vec()),
|
body: Some("{}".as_bytes().to_vec()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
})?;
|
})?;
|
||||||
@@ -83,7 +83,7 @@ pub struct FetchHeader {
|
|||||||
#[derive(Clone, Debug, Default)]
|
#[derive(Clone, Debug, Default)]
|
||||||
pub struct FetchOptions {
|
pub struct FetchOptions {
|
||||||
pub method: FetchMethod,
|
pub method: FetchMethod,
|
||||||
pub headers: Option<Vec<FetchHeader>>,
|
pub headers: Vec<FetchHeader>,
|
||||||
pub body: Option<Vec<u8>>,
|
pub body: Option<Vec<u8>>,
|
||||||
// FIXME connect timeout or read timeout?
|
// FIXME connect timeout or read timeout?
|
||||||
pub timeout: Option<Duration>,
|
pub timeout: Option<Duration>,
|
||||||
@@ -135,9 +135,9 @@ pub fn fetch(url: &str, option: &FetchOptions) -> XResult<FetchResponse> {
|
|||||||
if let Some(timeout) = &option.timeout {
|
if let Some(timeout) = &option.timeout {
|
||||||
let _ = request.timeout_mut().insert(timeout.to_owned());
|
let _ = request.timeout_mut().insert(timeout.to_owned());
|
||||||
}
|
}
|
||||||
if let Some(headers) = &option.headers {
|
if !option.headers.is_empty() {
|
||||||
let headers_mut = request.headers_mut();
|
let headers_mut = request.headers_mut();
|
||||||
for header in headers {
|
for header in &option.headers {
|
||||||
let header_name = HeaderName::from_str(&header.key)?;
|
let header_name = HeaderName::from_str(&header.key)?;
|
||||||
let header_value = HeaderValue::from_str(&header.value)?;
|
let header_value = HeaderValue::from_str(&header.value)?;
|
||||||
headers_mut.insert(header_name, header_value);
|
headers_mut.insert(header_name, header_value);
|
||||||
|
|||||||
Reference in New Issue
Block a user