Google App Engine: заголовок ответа "Content-Length" всегда равен 0
Я следовал учебному пособию по Google App Engine, и у меня возникли некоторые проблемы с добавлением контента к объекту ответа в классе гостевой книги.
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)
Используя Wireshark, вот ответный пакет:
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
Как вы можете видеть, я пытаюсь заполнить тело контента "Hello", но оно постоянно дает мне content-length = 0 и ручная настройка, кажется, не помогает, поэтому я прокомментировал это. Я думаю, что вы можете смело игнорировать код с приветствием, но я добавил его на случай, если он повлияет на все, что я делаю.
1 ответ
Вы отправляете перенаправление перед печатью какого-либо контента на страницу, следовательно, длина 0.
self.redirect('/?' + urllib.urlencode(query_params)) # redirects
print type(self.response) # never executes
Если вы посмотрите на перехват Wireshark, проверьте код ответа, который является перенаправлением 302 и Location:
заголовок, который сообщает браузеру, куда перенаправить.
HTTP/1.1 302 Found
Location: http://_______.appspot.com/?guestbook_name=default_guestbook