Ross Rogers
Ross Rogers

Reputation: 24228

generating non-html output from google's app engine

I'm trying to dynamically generate iCal output using Max M's icalendar python module and google's app engine. However, when I omit the boiler plate <html> header and footer tags on the output from webapp.RequestHandler, it looks like the HTTP 200 response is added to the top of the file.

When I register this code:

class Calendar(webapp.RequestHandler):
    def get(self):
        self.response.out.write('BEGIN:VCALENDAR\n')
        [...]

with webapp.WSGIApplication, the response looks like:

Status: 200 OK
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Content-Length: 11133

BEGIN:VCALENDAR
[...]

How do I omit the <html> tags and not get the HTTP 200 response splatted at the top of the page?

Upvotes: 1

Views: 771

Answers (5)

user670877
user670877

Reputation: 1

Something else to consider:

If you are importing code from another file, and that other file is a webapp, you need to make sure that it only runs as a webapp if it is the main

Anotherwords - Go from this

application = webapp.WSGIApplication(
                                 [
                                  ('/locate', Request),
                                   ],
                                 debug=False)

util.run_wsgi_app(application)

to this

application = webapp.WSGIApplication(
                                 [
                                  ('/locate', Request),
                                   ],
                                 debug=False)

def main():
   util.run_wsgi_app(application)

if __name__ == "__main__":
   main()

This problem was killing me for hours, and this was one of the only posts I could find that was close to it. I'm hoping this will help anyone else with the same problem as me!

Upvotes: 0

Nick Johnson
Nick Johnson

Reputation: 101149

Webapp and App Engine don't care what content type you return, so the issue is unrelated to the tag (or lack thereof). You likely have a print statement somewhere in your code, which is causing the headers to be sent as part of the body of the response. You should never use print in a WSGI app - always use self.response.out.write as in the snippet you pasted.

Upvotes: 3

dplouffe
dplouffe

Reputation: 565

You can't, the "Status: 200 Ok....." is the HTTP header which will be outputted on every request by the web server. Any web server will do the same thing.

Your client will need to read the body of the http response.

Upvotes: 2

bgporter
bgporter

Reputation: 36504

Not sure if this is the real problem or not, but you should be setting the content-type to indicate that this is calendar data, not HTML:

Content-Type: text/html; charset=utf-8

should be

Content-Type: text/calendar;

Upvotes: 1

nefo_x
nefo_x

Reputation: 3088

This seems to be outputting standard HTTP response headers. Try adding "Content-Disposition: attachment; filename='%s'" % filename HTTP header to your response to get desired file downloaded.

Upvotes: 0

Related Questions