Question

I followed the Google App Engine tutorial and I'm having a bit of trouble with adding content to the response object in the guestbook class.

class Guestbook(webapp2.RequestHandler):
def post(self):

    # We set the same  parent key on the 'Greeting' to ensure each greeting
    # is in the same entity group. Queries across the single entity group
    # will be consistent. However, the write rate to a single entity group
    # should be limited to ~1/second.
    guestbook_name = self.request.get('guestbook_name',
                                      DEFAULT_GUESTBOOK_NAME)
    testvar = self.request.get('testvar',
                                      DEFAULT_GUESTBOOK_NAME)
    greeting = Greeting(parent=guestbook_key(guestbook_name))

    if users.get_current_user():
        greeting.author = users.get_current_user()

    greeting.content = self.request.get('content')
    greeting.info = 'DIDTHISWORK?'
    greeting.put()

    self.response.headers.add_header("Expires", 'Information here')      
    #self.response.set_status(200,'Is this working?!')

    self.response.headers['Content-Type'] = 'text/plain'
    #self.response.headers['Content-Length'] = '5'
    self.response.out.write('Hello')


    query_params = {'guestbook_name': guestbook_name}
    self.redirect('/?' + urllib.urlencode(query_params))
    print type(self.response)

Using Wireshark here is the response packet:

HTTP/1.1 302 Found
Cache-Control: no-cache
Expires: Information here
Content-Type: text/plain
Location: http://_______.appspot.com/?guestbook_name=default_guestbook
Date: Fri, 17 May 2013 01:21:52 GMT
Server: Google Frontend
Content-Length: 0

As you can see I'm trying to fill the content body with "Hello" but it keeps giving me content-length = 0 and manually setting it doesn't seem to help so I commented it out. I think you can safely ignore the code with greeting but I added it in there in case it affects anything I do.

Was it helpful?

Solution

You are sending a redirect before you print any content to the page, hence the 0 length.

self.redirect('/?' + urllib.urlencode(query_params)) # redirects
print type(self.response)                            # never executes

If you look at the Wireshark capture, check out the response code which is a 302 redirect and the Location: header which tells the browser where to redirect to.

HTTP/1.1 302 Found 
Location: http://_______.appspot.com/?guestbook_name=default_guestbook
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top