开发者

Google App Engine HTTP information being printed somehow

开发者 https://www.devze.com 2023-01-28 03:00 出处:网络
I\'m just wanting to return a JSON object, but HTTP information is being printed below it. I\'m using Google App Engine and https://github.com/simplegeo/python-oauth2

I'm just wanting to return a JSON object, but HTTP information is being printed below it. I'm using Google App Engine and https://github.com/simplegeo/python-oauth2

#!/usr/bin/env python

from google.a开发者_如何学运维ppengine.ext import webapp
from google.appengine.ext.webapp import util
from django.utils import simplejson as json
import oauth2 as oauth
import cgi

class MainHandler(webapp.RequestHandler):

    def get(self):

  consumer = oauth.Consumer(key="xxx", 
      secret="xxx")

  request_token_url = "xxx"

  client = oauth.Client(consumer)

  resp, content = client.request(request_token_url, "POST")

  if resp['status'] != '200':
      raise Exception("Invalid response %s." % resp['status'])

  request_token = dict(cgi.parse_qsl(content))
  print
  print json.dumps({"oauth_token": request_token['oauth_token'], "oauth_token_secret": request_token['oauth_token_secret']})

def main():
    application = webapp.WSGIApplication([('/', MainHandler)],
                                         debug=True)
    util.run_wsgi_app(application)


if __name__ == '__main__':
    main()


Add the proper Content-Type and switch to self.response.out.write

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


Instead of print use self.response:

 self.response.out.write("Some Text")

I suppose that the RequestHandler automatically prints a default HTTP header and sends it if nothing is written to the response.out stream.

If you only want to send JSON data you can set the "Content-Type" header information to "application/json".

0

精彩评论

暂无评论...
验证码 换一张
取 消