Search code examples
python-2.7google-app-enginewebapp2

What is python equivalent of php file_get_contents("php://input")


I am trying to get post request raw data in python, can anyone tell me how? OR the equivalence method of file_get_contents("php://input") in Python?


Solution

  • To get a POST request's 'raw' data you do

    class YourRequestHandler(webapp2.RequestHandler):
        def post(self):
            body = self.request.body
    

    body now contains the POST request body where the request parameters are typically stored. This is usually a key-value pair which can be translated into a python dictionary for you with json.loads(self.request.body)