Reqw
⚠️ Warning: This crate is only published to provide documentation on docs.rs. It is not intended to be used via
cargo add. Please copy the code manually into your project.
Note: This is not a library — just a code snippet you can copy-paste into your project. Why? Because Rust treats each crate version as a unique type. If this helper were a real library and depended on
reqwest, it would only work when your project uses exactly the same version ofreqwest. Supporting every version would require releasing multiple versions of this crate — which is overkill for a tiny helper like this. So instead, it’s just a clean, dependency-free snippet you can drop into your own codebase.
How to use
Save the following code as reqw.rs somewhere in your project (e.g., in src/utils/reqw.rs):
/// This function is re-estimates response of reqwest
///
/// - Returns `Ok(reqwest::Response)` if status code is 200-299
/// - Returns `Err(Error::Http)` if the status code is not 2xx
/// - Returns `Err(Error::Transport)` if there was a transport error
///
/// # Examples
///
/// ```rust
/// async fn run() {
/// let result = reqw::est(reqwest::get("https://example.com").await);
/// assert!(result.is_ok());
/// }
/// ```
Then, use it like this:
use reqw;
async
What Reqw::est() does
Reqw::est() stands for "estimate" — it redefines how to interpret reqwest's Result<Response, Error>
by re-estimating the outcome of an HTTP request into a simpler, cleaner success/error model that better fits real-world application logic.
In reqwest, the result of an HTTP request looks like this:
This has three possible real-world meanings:
- ✅ Ok(res) where res.status().is_success() (e.g., 200 OK)
- ✅ Ok(res) where res.status() is an error (e.g., 404, 500)
- ❌ Err(e) for transport-level failures (e.g., timeout, DNS failure)
By default, reqwest treats all Ok(Response) as "success", even if the server returned a 500 Internal Server Error — because the HTTP protocol worked.
Reqw::est() reinterprets that like this:
With:
- ✅ Ok(res) → only if the response is a 2xx success
- ❌ Err(reqw::Error::Http(reqwest::Response)) → if response is non-2xx
- ❌ Err(reqw::Error::Transport(reqwest::Error)) → if the request itself failed (network error, timeout, etc.)
This way, you can treat HTTP error responses in the same way as transport errors — making your control flow simpler and more idiomatic in Rust.