This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: urllib2.Request() will not work with long authorization headers
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.6
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: orsenthil Nosy List: jamal, nirai, orsenthil
Priority: normal Keywords:

Created on 2009-07-09 20:26 by jamal, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg90351 - (view) Author: Jamal Fanaian (jamal) Date: 2009-07-09 20:26
While testing this bug report in Gwibber I found that using
urllib2.Request() with a long authorization would cause it to error and
not authenticate. I'm not sure if it is because the header wasn't being
passed or any other issue.

The header would fail when the value part of the authorization string
was 89 characters.

I was able to resolve the issue by using urllib2.HTTPBasicAuthHandler()
and urllib2.install_opener() to install it, instead of passing the
Authorization header directly to urllib2.Request().

Please let me know if I can provide more information to test the issue.
msg99940 - (view) Author: Senthil Kumaran (orsenthil) * (Python committer) Date: 2010-02-23 17:42
There is a OLD Known issue that base64.encodestring will insert in a line-break in the authorization header. See Issue1574068
I fear, you might have done something like this.

base64string = base64.encodestring('%s:%s' %(username,passwd))[:-1]
request.add_header("Authorization","Basic %s" % base64string)

You might have encountered this problem of Request not working with long authorization headers.
Instead, if you do

base64string = base64.b64encode('%s:%s' %(username,passwd))[:-1]

and then add the string to your headers, it might work properly. It does so for me.

(alternatively you can set base64.MAXBINSIZE=1000000, but the first solution is much cleaner.

I am closing this bug as Invalid. If you feel, it is valid and you used base64.b64encode for encoding the Basic Authentication header. feel free to reopen the bug, with a snippet of code attached.
Date User Action Args
2022-04-11 14:56:50adminsetgithub: 50701
2010-02-23 17:42:27orsenthilsetstatus: open -> closed
resolution: not a bug
messages: + msg99940
2009-12-27 18:19:55niraisetnosy: + nirai
2009-12-27 16:23:21orsenthilsetpriority: normal
2009-11-30 14:14:36orsenthilsetassignee: orsenthil

nosy: + orsenthil
2009-07-09 20:26:50jamalcreate