wkgrcdsam
wkgrcdsam

Reputation: 332

requests - ChunkedEncodingError with requests.get, but not when using Postman

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

Upvotes: 11

Views: 29901

Answers (1)

Rajan jha
Rajan jha

Reputation: 390

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)}")

Upvotes: 15

Related Questions