williamparry
williamparry

Reputation: 488

Google App Engine HTTP information being printed somehow

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.appengine.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()

Upvotes: 1

Views: 177

Answers (2)

fgm2r
fgm2r

Reputation: 4520

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".

Upvotes: 1

systempuntoout
systempuntoout

Reputation: 74114

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

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

Upvotes: 2

Related Questions