Python: BaseHTTPRequestHandler - read the original entry

As I read the raw post http STRING. I found several solutions for reading the parsed version of the message, however the project I'm working on is sending the raw xml payload without a header. Therefore, I am trying to find a way to read the message data without analyzing it into an array of key => values.

+6
source share
2 answers

I think self.rfile.read(self.headers.getheader('content-length')) should return 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; 
+11
source

self.rfile.read(int(self.headers.getheader('Content-Length'))) returns the original HTTP POST data as a string.

Destruction:

  • The 'Content-Length' header indicates how many bytes the HTTP POST data contains.
  • self.headers.getheader('Content-Length') returns the length of the content (header value) as a string.
  • This needs to be converted to an integer before passing as a parameter to self.rfile.read() , so use the int() function.

Also, note that the header name is case sensitive, therefore it only has “Content-Length”.

Edit: Apparently, the header field is not case sensitive (at least in Python 2.7.5), which I believe is the correct behavior since https://tools.ietf.org/html/rfc2616 :

Each header field consists of a name followed by a colon (":") and a field value. Field names are case insensitive.

+13
source

Source: https://habr.com/ru/post/950386/


All Articles