How to extract HTTP message body in BaseHTTPRequestHandler.do_POST()?

PythonHttpPostBasehttpserverBasehttprequesthandler

Python Problem Overview


In the do_POST() method of BaseHTTPRequestHandler I can access the headers of the POST request simply via the property self.headers. But I can't find a similar property for accessing the body of the message. How do I then go about doing that?

Python Solutions


Solution 1 - Python

You can access POST body in do_POST method like this:

for python 2

content_len = int(self.headers.getheader('content-length', 0))

for python 3

content_len = int(self.headers.get('Content-Length'))

and then read the data

post_body = self.rfile.read(content_len)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFrederick The FoolView Question on Stackoverflow
Solution 1 - PythonRoman BodnarchukView Answer on Stackoverflow