Close

2023-10-08

cannot unpack non-iterable Response object

cannot unpack non-iterable Response object

The error message “cannot unpack non-iterable Response object” typically arises in Python when trying to unpack or destructure a response object as if it were iterable (like a tuple or a list). Still, it’s not designed to be unpacked.

For instance, consider a scenario where you’re making an HTTP request using the requests library:

import requests

response = requests.get('https://example.com')
data, status_code = response

The above code will throw the error because the response object is not iterable in a way that allows it to be unpacked into data and status_code.

Solution:

To fix this, you should access the attributes of the Response object directly:

import requests

response = requests.get('https://example.com')
data = response.text  # or response.json() if dealing with JSON data
status_code = response.status_code

Always ensure you’re working with the correct type of object and accessing its attributes or methods in the way they’re intended to be used.

https://ozgurozkok.com/mastering-python-programming-a-comprehensive-guide-to-essential-topics/