HTTP client: fixes POST requests

Raises an error message on not 2XX responses
pull/216/head
Tpt 3 years ago
parent 5a3b1e3b36
commit d9487fd9f5
  1. 26
      lib/src/sparql/http/simple.rs
  2. 2
      server/src/main.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)))?

@ -1029,7 +1029,7 @@ impl<O, U: (Fn(O) -> std::io::Result<Option<O>>)> Read for ReadForWrite<O, U> {
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())?;

Loading…
Cancel
Save