Python: BaseHTTPRequestHandler - Read raw post
I think self.rfile.read(self.headers.getheader('content-length'))
should return the raw data as a string.
According to the docs directly inside the BaseHTTPRequestHandler class:
- rfile is a file object open for reading positioned at the
start of the optional input data part;
self.rfile.read(int(self.headers.getheader('Content-Length')))
will return the raw HTTP POST data as a string.
Breaking it down:
- The header 'Content-Length' specifies how many bytes the HTTP POST data contains.
self.headers.getheader('Content-Length')
returns the content length (value of the header) as a string.- This has to be converted to an integer before passing as parameter to
self.rfile.read()
, so use theint()
function.
Also, note that the header name is case sensitive so it has to be specified as 'Content-Length' only.
Edit: Apparently header field is not case sensitive (at least in Python 2.7.5) which I believe is the correct behaviour since https://www.rfc-editor.org/rfc/rfc2616 states:
Each header field consists of a name followed by a colon (":") and the field value. Field names are case-insensitive.