Skip to content

content-length header value not available after get #2731

@medington

Description

@medington

I'm unable to get content-length reliably with the requests library whereas urllib2 seems to retrieve the data consistently. Not sure if additional code is required or if the URL I'm hitting is problematic.

Run this script and compare the results. I consistently get None from requests. Using the other test_url I consistently get a result from both. What am I missing?

# Test which demonstrates issues with requests get of content-length header

# This URL seems problematic (it's a several minute download)
test_url = "http://standards-oui.ieee.org/oui.txt"
# test_url = 'https://github.com/kennethreitz/requests/tarball/master'

def requests_getlen(url):
    import requests
    r = requests.get(url, stream=True)
    return str(r.headers.get('content-length'))

def urllib_getlen(url):
    import urllib2
    req = urllib2.urlopen(url)
    return str(req.info().getheader('content-length'))

print 'requests content-length: ' + requests_getlen(test_url)
print 'urllib2  content-length: ' + urllib_getlen(test_url)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions