WSGI - set content type to JSON

I am insanely green for WSGI in the Google App Engine (GAE).

How to set content type in JSON? This is what I still have:

class Instructions(webapp.RequestHandler): def get(self): response = {} response["message"] = "This is an instruction object" self.response.out.write(json.dumps(response)) application = webapp.WSGIApplication([('/instructions', Instructions)], debug=True) def main(): run_wsgi_app(application) if __name__ == "__main__": main() 

In addition, I create several RESTful services, nothing complicated. When I developed at JAVA, I used retouching. Is there a more efficient framework than WSGI? The only reason I use WSGI is because they were used in the App Engine tutorial.

Thanks!

+7
source share
3 answers

You can set the correct Content-Type like this:

 self.response.headers['Content-Type'] = "application/json" self.response.out.write(json.dumps(response)) 

WSGI is not a framework, but a specification; The framework you are currently using is the webapp framework.

There is nothing complicated and specific like Restlet on the Python side; however, with webapp, you can create RESTful request handlers using regular expressions that return JSON / XML data similar to your handler.

+14
source

Like any HTTP response, you can add or edit headers:

 def get(self): response = {} response["message"] = "This is an instruction object" self.response.headers["Content-Type"] = "application/json" self.response.out.write(json.dumps(response)) 

Read more here: Redirects, headers and status codes

+2
source

Is there a more efficient framework than WSGI?

Take a look at Pyramid (previously named pylons, if you notice this). Seems like it would be better in your case against django.

+1
source

All Articles