Craig
Craig

Reputation: 2143

How do I convert Python POST data to JSON?

I have the following code in Python:

def post(self):
    example = self.request.get("example")
    other = self.request.get("other")

How do I get all post data if unknown? I'm very new to Python but something along the lines of:

def post(self):
    array = self.request.get()
    myJSON = MagicArrayToJson(array)
    self.response.headers['Content-Type'] = 'application/json'
    self.response.write(myJSON)

Upvotes: 2

Views: 823

Answers (1)

privetartyomka
privetartyomka

Reputation: 85

It may depend on the framework you use. But i suppose that there all have pretty same notation like self.request.data or self.request.body for post request.

Try self.request.data like this

def post(self):
    data = self.request.data

    # If data is in querystring, convert it to dict
    # urlparse lib is convenient for this

    myJSON = MagicArrayToJson(array)
    self.response.headers['Content-Type'] = 'application/json'
    self.response.write(myJSON)

Upvotes: 2

Related Questions