1

I am getting Error from Requests Module. My same code was working few days back, But its not working anymore.

File "C:\Program Files (x86)\Python38-32\lib\site-packages\requests\adapters.py", line 469, in send
            for i in request.body:
        TypeError: 'function' object is not iterable

Python=3.8

requests=2.22.0

Full Code

C:\Windows\system32>python
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:21:23) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import json, requests
>>> from requests.auth import HTTPBasicAuth
>>> url = "https://mywebsite.com/"
>>> auth = HTTPBasicAuth("username", "password")
>>> headers = { "Accept": "application/json","Content-Type": "application/json"}
>>> tickets_created = []
>>> payload = json.dumps
>>> (
...     {
...         "fields":
...         {
...         "project":
...             {
...             "key": "PROJECT_A"
...             },
...         "summary": "summary",
...         "description": "desc",
...         "issuetype":
...             {
...             "name": "Incident",
...             },
...         "components":
...     [{"name":"Active Directory" }],
...         "assignee":
...             {
...             "name":"[email protected]"
...             }
...        }
...     }
... )
{'fields': {'project': {'key': 'PROJECT_A'}, 'summary': 'summary', 'description': 'desc', 'issuetype': {'name': 'Incident'}, 'components': [{'name': 'Active Directory'}], 'assignee': {'name': '[email protected]'}}}
>>> response = requests.request("POST",url,data=payload,headers=headers,auth=auth,verify=False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\requests\api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\requests\sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\requests\sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\requests\adapters.py", line 469, in send
    for i in request.body:
TypeError: 'function' object is not iterable
>>>
2
  • This isn't a minimal reproducible example so I don't see how we can help. Commented Sep 23, 2020 at 15:08
  • @RandomDavis, Added full code Commented Sep 23, 2020 at 15:23

3 Answers 3

3

The issue is that you're setting payload = json.dumps - all the lines after it are not being passed to dumps(). Do this instead (putting the open parenthesis on the same line):

payload = json.dumps(
    {
        "fields":
        {
        "project":
            {
            "key": "PROJECT_A"
            },
        "summary": "summary",
        "description": "desc",
        "issuetype":
            {
            "name": "Incident",
            },
        "components":
    [{"name":"Active Directory" }],
        "assignee":
            {
            "name":"[email protected]"
            }
       }
    }
)
Sign up to request clarification or add additional context in comments.

3 Comments

It Worked, Thank you. But I am still Concerned, because My same code was working few days back.
@Aashutosh if it was working before, something about it must have been different.
Only Difference is, I was using this piece of code in a function, To debug something, when I am using this independently, It was giving error.
0

It seems that the variable auth that you are using is a function and not a tuple or list.

The docs for requests.request() say:

:param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.

Comments

0

Try this:

requests.post(url,data=payload, headers=headers, auth=auth, verify=False)

2 Comments

Can you delate auth and verify?
Problem is identified, Issue is the json.dumps Opening Bracket. Since I have SSL Certificate Error at Target Site, I have to use verify=False

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.