1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use anyhow::{Context, Result};
pub async fn http_post<U: AsRef<str>, B: Into<reqwest::Body>>(url: U, body: B) -> Result<String> {
let url = url.as_ref();
info!("HTTP POST to {}", url);
let resp = reqwest::Client::new()
.post(url)
.body(body)
.send()
.await
.with_context(|| url.to_string())?;
let status = resp.status();
let text = resp.text().await.with_context(|| url.to_string())?;
if status.is_client_error() || status.is_server_error() {
bail!("HTTP error {}: {}", status, text);
}
Ok(text)
}
pub async fn http_get<I: AsRef<str>>(url: I) -> Result<Vec<u8>> {
let url = url.as_ref();
info!("HTTP GET {}", url);
let resp = reqwest::get(url).await?.error_for_status()?.bytes().await?;
Ok(resp.to_vec())
}