How to iterate over all request headers in webapp RequestHandler using python?

I need to iterate over all request header objects and print it in App Engine. I get an error when trying to use for a loop. How to do it right?

class MainHandler(webapp.RequestHandler):
    def get(self):
        for e in self.request.headers:
            self.request.headers(e + "<br />")

I get an error: AttributeError: EnvironHeaders instance has no __call__ method

+5
source share
2 answers

The error is on the line self.request.headers(e + "<br />"). You are trying to call a method request.headers.

I check the online help and discover what self.request.headersis there dictas an object. You can check https://developers.google.com/appengine/docs/python/gettingstarted/usingwebapp

To iterate through headers, you can use self.request.headers.items()orself.request.headers.keys()

+8
source

I think you mean self.response.write():

class MainHandler(webapp.RequestHandler):
    def get(self):
        for e in self.request.headers:
            self.response.write(e + "<br />")
0
source

All Articles