From 38b8fbe37fd9be888ce69fc4c3a6bbd0b7735376 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 4 Jun 2023 14:20:40 +0800 Subject: [PATCH] feat: udpate fetch-rs --- __network/fetch-rs/README.md | 4 ++-- __network/fetch-rs/src/main.rs | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/__network/fetch-rs/README.md b/__network/fetch-rs/README.md index d1216cc..77b60f1 100644 --- a/__network/fetch-rs/README.md +++ b/__network/fetch-rs/README.md @@ -22,12 +22,12 @@ let response = fetch( "https://hatter.ink/util/print_request.action", &FetchOptions { method: FetchMethod::Post, - headers: Some(vec![ + headers: vec![ FetchHeader { key: "content-type".to_string(), value: "application/json".to_string(), } - ]), + ], body: Some("{}".as_bytes().to_vec()), ..Default::default() })?; diff --git a/__network/fetch-rs/src/main.rs b/__network/fetch-rs/src/main.rs index 9658850..f0a3803 100644 --- a/__network/fetch-rs/src/main.rs +++ b/__network/fetch-rs/src/main.rs @@ -28,12 +28,12 @@ fn main() -> XResult<()> { "https://hatter.ink/util/print_request.action", &FetchOptions { method: FetchMethod::Post, - headers: Some(vec![ + headers: vec![ FetchHeader { key: "content-type".to_string(), value: "application/json".to_string(), } - ]), + ], body: Some("{}".as_bytes().to_vec()), ..Default::default() })?; @@ -83,7 +83,7 @@ pub struct FetchHeader { #[derive(Clone, Debug, Default)] pub struct FetchOptions { pub method: FetchMethod, - pub headers: Option>, + pub headers: Vec, pub body: Option>, // FIXME connect timeout or read timeout? pub timeout: Option, @@ -135,9 +135,9 @@ pub fn fetch(url: &str, option: &FetchOptions) -> XResult { if let Some(timeout) = &option.timeout { 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(); - for header in headers { + for header in &option.headers { let header_name = HeaderName::from_str(&header.key)?; let header_value = HeaderValue::from_str(&header.value)?; headers_mut.insert(header_name, header_value);