我对Google App Engine(GAE)上的WSGI感到疯狂。
如何将内容类型设置为JSON?这就是我到目前为止所做的:
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()
此外,我正在构建一些RESTful服务,没有太复杂。我在JAVA开发时使用的是restlets。是否有比WSGI更好的框架?我使用WSGI的唯一原因是因为这是他们在App Engine教程中使用的。
谢谢!
答案 0 :(得分:14)
您可以使用以下内容设置正确的 Content-Type :
self.response.headers['Content-Type'] = "application/json"
self.response.out.write(json.dumps(response))
WSGI不是框架,而是规范;您当前使用的框架是webapp框架。
在Python方面没有像Restlet那样复杂和具体的东西;但是使用webapp,您可以通过正则表达式创建RESTful request handlers,像处理程序一样返回JSON / XML数据。
答案 1 :(得分:2)
与任何HTTP响应一样,您可以添加或编辑标题:
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))
答案 2 :(得分:1)
使用比WSGI更好的框架吗?
看看金字塔(以前称为挂架,如果你看到提到的话)。听起来对你的django来说会更好。