adamdruppe / arsd

This is a collection of modules that I've released over the years. Most of them stand alone, or have just one or two dependencies in here, so you don't have to download this whole repo.
http://arsd-official.dpldocs.info/arsd.html
530 stars 125 forks source link

Empty-body POSTs don't get a Content-Length header #328

Closed zorael closed 2 years ago

zorael commented 2 years ago

I was attempting to access the Google API1, which at one point requires a POST request with some query params (where the body can be left empty) to get an OAuth access token.

auto client = new HttpClient;
const url = "https://oauth2.googleapis.com/token?client_id=[stuff]&client_secret=[stuff]&code=[stuff]&grant_type=authorization_code&redirect_uri=[stuff]"
// ...
auto req = client.request(Uri(url), HttpVerb.POST);
auto res = req.waitForCompletion();
<!DOCTYPE html>                                                                            
<html lang=en>                                                                             
  <meta charset=utf-8>                                                                                                                                                                 
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">      
  <title>Error 411 (Length Required)!!1</title>                                                                                                                                        
  <style> [...]
  </style>
  <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
  <p><b>411.</b> <ins>That’s an error.</ins>
  <p>POST requests require a <code>Content-length</code> header.  <ins>That’s all we know.</ins>

I could work around it by passing a dummy body ubyte[] with just "{}", or by manually setting a Content-Length: 0 header.

auto req = client.request(Uri(url), HttpVerb.POST);
req.requestParameters.headers = [ "Content-Length: 0" ];
auto res = req.waitForCompletion();

Should empty-body POSTs get a Content-Length too, or am I doing it wrong? Should I expect to have to work around it in these cases?

adamdruppe commented 2 years ago

I'd have to double check the spec but it is probably my bug. I have

            if(requestParameters.bodyData.length)

And it should probably instead be if the request is a POST or PUT or PATCH.... so this was jsut my shortcut cuz I wouldn't set things otherwise.

adamdruppe commented 2 years ago

https://httpwg.org/specs/rfc7230.html#header.content-length

ok yeah the spec says it should be there for the posts and such, but not there for gets. let me check the others then push up a patch.

adamdruppe commented 2 years ago

test it with the master now plz. you can see in the commit it was an easy patch

zorael commented 2 years ago

Yes, it seems to work now. Thanks!

adamdruppe commented 2 years ago

excellent.