简体   繁体   中英

Receiving A HTTP POST Request

What are the rules for receiving a HTTP 1.1 request? I'v tried looking at multiple sources of information but I can never come to a conclusive result. In particular I'm interested in figuring out how to handle the body of the POST request. I am currently creating my own web service which does not use PHP or ASP.net. My website currently handles Get Requests and the POST header fine but never receives the body data from the client/web browser, thus is why I want to find out if there are any special rules for receiving the body of a POST.

Hope you can help

Thanks, Mitchell

There aren't any special rules for handling a POST , except that the sender needs to give it to you in a format you're expecting. Specifically, you can get tripped up if the POST request is coming in a format not put up in the "ACCEPT" header of the request. I'm not sure how you're doing this, but that is the most common foul-up i've encountered. Example ACCEPTs are text/plain, text/json, etc. You can read up on this a little at https://en.wikipedia.org/wiki/List_of_HTTP_header_fields which will go over the common set. Don't forget you have the option of implementing your own if you really want to, which is great for things like API keys (though using an established standard is considered best practice).

Likewise, you may be running afoul of Cross-Origin Request restrictions. CORS will restrict a client from receiving requests from another domain, usually to protect the user from Very Bad Things (TM) that can happen.

Though, this is the benefit of using PHP and ASP.NET, a lot of the weirdness that goes along with HTTP requests gets handled for you by the language (PHP) or the framework (ASP.NET), and I can speak to ASP.NET being an excellent gatekeeper of malformed requests.

I stumbled across this question as I, too, was unable to receive a HTML POST body. In my case, I wanted to receive a file from a form like this:

<form action="http://localhost:8000/" method="post" enctype="multipart/form-data">
  <input type="file" /> 
  <input type="submit" value="OK" />
</form>

For debugging purposes, I started

nc -l localhost 8000

to receive and display the request as described in What does enctype='multipart/form-data' mean? .

The observant reader might notice that the type="file" input is missing the name attribute. As a result, the browsers I used, namely

  • Mozilla Firefox 53.0
  • Google Chrome 58.0.3029.96

, silently fail to include the file data in the POST request body. The Content-Length field reflects this. But netcat on the receiving end does not parse the field, of course. As a result, both parties simply keep the connection open.

Note: This question seemed appropriate for my findings. @mitchell-nursey's other question Web Browser not sending Body of POST request until cancel of request looks like a "partially filled buffer not read after disconnect"-error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM