11

I have a URL that can only be accessed from within our VPC.

If I enter the URL via Postman, I am able to see the content (which is a Pdf) and I'm able to save the output to a file perfectly fine.

However, when trying to automate this using python requests with

import requests r = requests.get(url, params=params) 

I get an exception

ChunkedEncodingError: ("Connection broken: InvalidChunkLength(got length b'', 0 bytes read)", InvalidChunkLength(got length b'', 0 bytes read)) 

The other Stackoverflow questions haven't really helped much with this and this is consistently reproducible with requests

1 Answer 1

17

I think the reason for this error is server is not correctly encoding the chunk. some of the chunk sizes are not integer b'' due to which you are getting chunk encoding error.

Try below code

import requests from requests.exceptions import ChunkedEncodingError response = requests.get(url, params=params, stream=True) try: for data in response.iter_content(chunk_size=1024): print(data) except ChunkedEncodingError as ex: print(f"Invalid chunk encoding {str(ex)}") 
Sign up to request clarification or add additional context in comments.

1 Comment

You may want to adjust the chunk_size. In fact I found chunk_size=64 worked in my case where 1024 or larger was encountering ChunkedEncodingError before transferred all. YMMV.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.