2
req = urllib2.Request
var1 = req.get_header('var1', default=None)
logging.info(var1)
var2 = req.get_header('var2', default=None)
logging.info(var2)
var3= req.get_header('var3', default=None)
logging.info(var3)
var4 = req.get_header('var4', default=None)
logging.info(var4)

Wrote the above python script to extract http headers, but getting following error when running on Google App Engine.

<type 'exceptions.TypeError'>: unbound method get_header() must be called with Request instance as first argument (got str instance instead)
Traceback (most recent call last):
  File "/base/data/home/apps/s~##############/##########/@@@@@@@@@.py", line 10, in <module>
    var1= req.get_header('var1', default=None)

New to Python development for App Engine, so unable to figure out the error.

5
  • Have a look at this earlier question about getheader. Commented Oct 28, 2014 at 7:26
  • Well, at the minimum you need to make an instance of Request like this req = urllib2.Request(url) Commented Oct 28, 2014 at 7:27
  • If the script for extracting headers is inside a web page(python coded). Using Advanced rest client(java based) I am sending the headers to the python web-page using GET. Getting the following error in app engine:: <class 'google.appengine.api.urlfetch_errors.InvalidURLError'>: App cannot fetch the same URL as the one used for the request. Commented Oct 28, 2014 at 10:35
  • I am trying to pass certain parameters from java web application to python script hosted on App Engine which uses those parameter's value for computation. The scenario for getting such error. Commented Oct 28, 2014 at 10:45
  • You don't need urllib2 at all, just do as @DanielRoseman said in his answer below Commented Oct 28, 2014 at 12:40

2 Answers 2

4

You seem to be doing something very strange: you are making a web request from your handler to the URL serving the same handler, and then trying to get the headers of the request you're actually making. Why would you do this? It makes no sense at all.

I think what you actually want to do is to get the headers from the request object that's already being passed to the handler, which of course is available via self.request.

Sign up to request clarification or add additional context in comments.

1 Comment

More specifically- self.request.headers (a dict). But yes what Daniel suggests here is the way to go.
2

You can get like this,

>>> req = urllib2.Request("http://www.google.com")
>>> response = urllib2.urlopen(req)
>>> response.info().getheader('Content-Type')
'text/html; charset=ISO-8859-9'
>>> 

1 Comment

If the script for extracting headers is inside a web page(python coded). Using Advanced rest client(java based) I am sending the headers to the python web-page using GET. Getting the following error in app engine:: <class 'google.appengine.api.urlfetch_errors.InvalidURLError'>: App cannot fetch the same URL as the one used for the request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.