Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting TypeError: 'int' when iterating trough data for my API

I am trying to iterate trough data from API and send them to my server but I am getting: TypeError: ‘int’ object is not iterable. Any idea how to solve it ?

Code for data source"

response = requests.get('https://jsonplaceholder.typicode.com/posts')
data = response.content

Code for iteration (BASE is variable for IP):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for i in range(len(data)):
    response = requests.put(BASE + "status/" + str(i), data[i])
    print(response.json())

Full Error:

Traceback (most recent call last):
  File "restApi/test.py", line 20, in <module>
    response = requests.put(BASE + "status/" + str(i), data[i])
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/requests/api.py", line 130, in put
    return request("put", url, data=data, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/requests/api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/requests/sessions.py", line 587, in request
    resp = self.send(prep, **send_kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/requests/sessions.py", line 701, in send
    r = adapter.send(request, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/requests/adapters.py", line 523, in send
    for i in request.body:
TypeError: 'int' object is not iterable

>Solution :

The problem is that you are using the content directly. You need to parse it before use it:

response = requests.get('https://jsonplaceholder.typicode.com/posts')
data = response.json()

for i in range(len(data)):
    response = print("status/" + str(i), data[i])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading