Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add empty body conversion from unit #2436

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

joshka
Copy link

@joshka joshka commented Sep 26, 2024

This makes it possible to convert an http::Request with an empty body by
passing () to the body method. This is useful when writing sans-io
libraries where you want to create requests that are agnostic to the
specific reqwest implementation (async, blocking, wasm).

E.g.:

let http_request = http::Request::builder()
    .uri("http://localhost/")
    .body(())?;

let req = reqwest::Request::try_from(http_request)?;
reqwest::Client::new().execute(req).await?;

// or
let req = reqwest::blocking::Request::try_from(http_request)?;
reqwest::blocking::Client::new().execute(req)?;

This makes it possible to convert an http::Request with an empty body by
passing `()` to the `body` method. This is useful when writing sans-io
libraries where you want to create requests that are agnostic to the
specific reqwest implementation (async, blocking, wasm).

E.g.:

```rust
let http_request = http::Request::builder()
    .uri("http://localhost/")
    .body(())?;

let req = reqwest::Request::try_from(http_request)?;
reqwest::Client::new().execute(req).await?;

// or
let req = reqwest::blocking::Request::try_from(http_request)?;
reqwest::blocking::Client::new().execute(req)?;
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant