Skip to content

POST requests without Content-Length or Transfer-Encoding return 400 Bad Request #2279

@o7si

Description

@o7si

I created a POST endpoint using the following code:

#include <httplib.h>
using namespace httplib;
int main(void) {
  Server svr;
  svr.Post("/test", [](const Request &req, Response &res) {
    res.set_content("", "text/plain");
  });
  svr.listen("0.0.0.0", 7777);
}

Then I accessed it using curl:

# curl -X POST "http://localhost:7777/test" -v
* Host localhost:7777 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
*   Trying [::1]:7777...
* connect to ::1 port 7777 from ::1 port 59000 failed: Connection refused
*   Trying 127.0.0.1:7777...
* Connected to localhost (127.0.0.1) port 7777
> POST /test HTTP/1.1
> Host: localhost:7777
> User-Agent: curl/8.7.1
> Accept: */*
> 
* Request completely sent off
< HTTP/1.1 400 Bad Request
< Connection: close
< Content-Length: 0
< 
* Closing connection

After analyzing the code, I found that the issue occurs at this location:

cpp-httplib/httplib.h

Lines 7934 to 7941 in 27ee115

if (req.method == "DELETE" && !req.has_header("Content-Length")) {
return true;
}
if (!detail::read_content(strm, req, payload_max_length_, res.status, nullptr,
out, true)) {
return false;
}

Based on my reading of RFC 7231, POST requests don't seem to require a message body, but I could be wrong about this. Should POST requests without Content-Length or Transfer-Encoding headers be accepted?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions