From d9487fd9f5550c3584c77fe912cb0fb911d0b1bd Mon Sep 17 00:00:00 2001 From: Tpt Date: Fri, 10 Jun 2022 17:01:09 +0200 Subject: [PATCH] HTTP client: fixes POST requests Raises an error message on not 2XX responses --- lib/src/sparql/http/simple.rs | 26 +++++++++++++++++++++++++- server/src/main.rs | 2 +- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/src/sparql/http/simple.rs b/lib/src/sparql/http/simple.rs index 5eee0aaa..a7eacc1f 100644 --- a/lib/src/sparql/http/simple.rs +++ b/lib/src/sparql/http/simple.rs @@ -24,6 +24,18 @@ impl Client { .map_err(invalid_input_error)? .build(); let response = self.client.request(request)?; + let status = response.status(); + if !status.is_successful() { + return Err(Error::new( + ErrorKind::Other, + format!( + "Error {} returned by {} with payload:\n{}", + status, + url, + response.into_body().to_string()? + ), + )); + } let content_type = response .header(&HeaderName::CONTENT_TYPE) .ok_or_else(|| invalid_data_error(format!("No Content-Type returned by {}", url)))? @@ -40,13 +52,25 @@ impl Client { content_type: &str, accept: &str, ) -> Result<(String, Body)> { - let request = Request::builder(Method::GET, url.parse().map_err(invalid_input_error)?) + let request = Request::builder(Method::POST, url.parse().map_err(invalid_input_error)?) .with_header(HeaderName::ACCEPT, accept) .map_err(invalid_input_error)? .with_header(HeaderName::CONTENT_TYPE, content_type) .map_err(invalid_input_error)? .with_body(payload); let response = self.client.request(request)?; + let status = response.status(); + if !status.is_successful() { + return Err(Error::new( + ErrorKind::Other, + format!( + "Error {} returned by {} with payload:\n{}", + status, + url, + response.into_body().to_string()? + ), + )); + } let content_type = response .header(&HeaderName::CONTENT_TYPE) .ok_or_else(|| invalid_data_error(format!("No Content-Type returned by {}", url)))? diff --git a/server/src/main.rs b/server/src/main.rs index 3cde7291..f363be8c 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1029,7 +1029,7 @@ impl std::io::Result>)> Read for ReadForWrite { self.state = match (self.add_more_data)(state) { Ok(state) => state, Err(e) => { - eprintln!("Internal server error while steaming: {}", e); + eprintln!("Internal server error while streaming results: {}", e); self.buffer .borrow_mut() .write_all(e.to_string().as_bytes())?;