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

Reqwest doesn't protect agains incorrect usage of HTTP verbs #2526

Open
Pzixel opened this issue Jan 11, 2025 · 0 comments
Open

Reqwest doesn't protect agains incorrect usage of HTTP verbs #2526

Pzixel opened this issue Jan 11, 2025 · 0 comments

Comments

@Pzixel
Copy link

Pzixel commented Jan 11, 2025

Most of existing HTTP client implementation have safeguards against incorrect usage of HTTP verbs. For example, following JS code:

fetch('https://jsonplaceholder.typicode.com/posts/1', {
    method: 'GET',
    body: JSON.stringify({a: 1}),
}).then((response) => {
    console.log(response);
});

Raises an error: TypeError: Request with GET/HEAD method cannot have body.

Most of clients in other languages do this as well.

It makes sense if following Rust code was also producing an error:

use url::Url;

#[tokio::main]
async fn main() {
    let url = Url::parse("https://jsonplaceholder.typicode.com/posts/1").unwrap();
    let client = reqwest::Client::new();
    let res = client.get(url).json(&serde_json::json!({"a": 1})).send().await.unwrap().text().await.unwrap();
    println!("{}", res);
}

The specification is pretty much against allowing such code to be executed:

9.3.1. GET A client SHOULD NOT generate content in a GET request unless it is made directly to an origin server that has previously indicated, in or out of band, that such a request has a purpose and will be adequately supported. An origin server SHOULD NOT rely on private agreements to receive content, since participants in HTTP communication are often unaware of intermediaries along the request chain.

While it might be valuable to keep a backdoor for extremely rare cases where server violates HTTP semantic and requires such requests, I think it is worth making this an error by-default.

I can work on an implementation if overall idea is supported by crate maintainers.

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

No branches or pull requests

1 participant