Can reproduce with the below:
import requests
r = requests.get('https://downloads.atlassian.com/software/confluence/downloads/atlassian-confluence-6.0.5.zip', stream=True)
print r.headers.get('content-length')
None
This results in None - I'm using content-length for a download progress bar and can't get it with requests ;(
If we use something else like urllib it returns it:
import urllib
r = urllib.urlopen(url).info()
print r
Content-Type: application/zip
Content-Length: 541131731
This used to work a while back, not sure if anything changed. Reproduced with Python 2.7.13 & 3.6.0 with requests 2.13.0.
If you look at the headers that are returned, you will notice
Transfer-Encoding: chunked
If you compare the headers that are sent, however, using httpbin, you might notice that Requests also sends Accept-Encoding: gzip, deflate. If you remove that header, e.g.,
r = requests.get(url, stream=True, headers={'Accept-Encoding': None})
Then you get your Content-Length header. This has been a header that's been set for quite a long time (I think since around when 1.0 was released) so you must have last used this years ago. I hope this helps.
Cheers!
Ian
@sigmavirus24 you're the best, thanks - I totally missed this. 馃憤
@sigmavirus24 sorry for mention. I still have this issue even when using your suggest.
for example when I want to get content-length from this image link
https://kenhsinhvien.vn/topic/conan-chap-699-tieng-viet.371791/ there is no result
Could you please give some advice?
@quyleanh it's possible that server doesn't send the content length ever. Some servers prefer not to calculate that if possible. Regardless, this isn't a question and answer support forum and you'll have better luck on stack overflow
@sigmavirus24 thank you for replying. Actually I have already asked on stackoverflow.
The thing is although I cannot get the Content-Length with get method, I still can get with head method. So the server sent the content length, right? Then what is the problem?
You can try with the link I send above.
You need to add Content-Encoding param , in my case only with this works, and content-length is not zero
r = requests.get(url, stream=True, headers={'Accept-Encoding': None, 'Content-Encoding':'gzip' })
Most helpful comment
If you look at the headers that are returned, you will notice
If you compare the headers that are sent, however, using
httpbin, you might notice that Requests also sendsAccept-Encoding: gzip, deflate. If you remove that header, e.g.,Then you get your
Content-Lengthheader. This has been a header that's been set for quite a long time (I think since around when 1.0 was released) so you must have last used this years ago. I hope this helps.Cheers!
Ian