Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to send the whole POST as a JSON object?

I am using GAE with python, and I am using many forms. Usually, my code looks something like this:

class Handler(BaseHandler):
    #...
    def post(self):
        name = self.request.get("name")
        last_name = self.request.get("last_name")
        # More variables...
        n = self.request.get("n")
        #Do something with the variables, validations, etc.
        #Add them to a dictionary
        data = dict(name=name, last_name=last_name, n=n)
        info = testdb.Test(**data)
        info.put()

I have noticed lately that it gets too long when there are many inputs in the form (variables), so I thought maybe I could send a stringified JSON object (which can be treated as a python dictionary using json.loads). Right now it looks like this:

class Handler(BaseHandler):
    #...
    def post(self):
        data = validate_dict(json.loads(self.request.body))
        #Use a variable like this: data['last_name']
        test = testdb.Test(**data)
        test.put()

Which is a lot shorter. I am inclined to do things this way (and stop using self.request.get("something")), but I am worried I may be missing some disadvantage of doing this apart from the client needing javascript for it to even work. Is it OK to do this or is there something I should consider before rearranging my code?

like image 842
Yamil Abugattas Avatar asked Sep 11 '25 08:09

Yamil Abugattas


1 Answers

There is absolutely nothing wrong with your short JSON-focused code variant (few web apps today bother supporting clients w/o Javascript anyway:-).

You'll just need to adapt the client-side code preparing that POST, from being just a traditional HTML form, to a JS-richer approach, of course. But, I'm pretty sure you're aware of that -- just spelling it out!-)

BTW, there is nothing here that's App Engine - specific: the same considerations would apply no matter how you chose to deploy your server.

like image 129
Alex Martelli Avatar answered Sep 13 '25 00:09

Alex Martelli