From 43457a3b04d4416c97e653163b78fa9322f356d0 Mon Sep 17 00:00:00 2001 From: "Jesper L. Nielsen" Date: Wed, 21 Feb 2024 13:13:40 +0100 Subject: [PATCH 1/2] fix(upload): return type on POST is now string A POST to a webserver can not always be expected to be a JSON response. Success is now determined by the HTTP return code. Upon success the body content is returned as a string. --- plugins/upload/src/lib.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/plugins/upload/src/lib.rs b/plugins/upload/src/lib.rs index 9d9966da29..c12039e999 100644 --- a/plugins/upload/src/lib.rs +++ b/plugins/upload/src/lib.rs @@ -93,7 +93,7 @@ async fn upload( url: &str, file_path: &str, headers: HashMap, -) -> Result { +) -> Result { // Read the file let file = File::open(file_path).await?; @@ -108,8 +108,13 @@ async fn upload( } let response = request.send().await?; - - response.json().await.map_err(Into::into) + if response.status().is_success() { + return response.text().await.map_err(Error::from); + } + Err(Error::ContentLength(format!( + "invalid status code: {}", + response.status().as_u16() + ))) } fn file_to_body(id: u32, window: Window, file: File) -> reqwest::Body { From abc5f42bc7b7c8533dd81b3e2edbcf66ec06a7e7 Mon Sep 17 00:00:00 2001 From: "Jesper L. Nielsen" Date: Thu, 29 Feb 2024 14:20:27 +0100 Subject: [PATCH 2/2] feat: add content-length on POST Not all embedded devices are acceptable to receiving unspecified amounts of data. Appending the content-length up front helps this devices succeed. --- plugins/upload/src/lib.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/plugins/upload/src/lib.rs b/plugins/upload/src/lib.rs index c12039e999..a5d9163dda 100644 --- a/plugins/upload/src/lib.rs +++ b/plugins/upload/src/lib.rs @@ -96,10 +96,14 @@ async fn upload( ) -> Result { // Read the file let file = File::open(file_path).await?; + let file_len = file.metadata().await.unwrap().len(); // Create the request and attach the file to the body let client = reqwest::Client::new(); - let mut request = client.post(url).body(file_to_body(id, window, file)); + let mut request = client + .post(url) + .header(reqwest::header::CONTENT_LENGTH, file_len) + .body(file_to_body(id, window, file)); // Loop trought the headers keys and values // and add them to the request object.