0

I am trying to use requests to post JSON data to a HTTP endpoint, but I get this weird errors now( I used it before with no problems).

Any troubleshooting is much appreciated.

The code:

req = requests.post(HTTP_ENDPOINT, data=json.dumps(data))

Output:

AttributeError: module 'requests' has no attribute 'post'

4
  • Please show full (related) code yo're running Commented Jan 29, 2019 at 15:27
  • 2
    Do you have a file named requests.py and import sth. from there? Commented Jan 29, 2019 at 15:27
  • Can you please provide all the import statements you have? Commented Jan 29, 2019 at 15:30
  • Here is the whole sampe code: ``` import requests HTTP_ENDPOINT = "endpoint1.test.com" data = {test:'123'} req = requests.post(HTTP_ENDPOINT, data=json.dumps(data)) print(requests.status_code) ``` I do have requests.py Commented Jan 29, 2019 at 15:33

3 Answers 3

1

If you have a file called requests.py in your folder, then python will import that as a module before the requests package that you've installed with pip.

That is why it says requests has no attribute 'post'. If you define a variable in your requests.py like this:

# requests.py
post = lambda *arg: print('unitended concequence')

You will likely see it print out that statement instead of complaining that post requests does not contain post. The solution is to rename your files so they don't shadow the packages you want to import. For instance change requests.py to my_requests.py.

Sign up to request clarification or add additional context in comments.

3 Comments

I don't have any file called requests.py in the folder. My script file is named something else
@Dart Try running print(requests.__file__) just prior to the line of code that fails with missing post attribute. If it's still pointing to the correct request it's likely going to print something like ....\site-packages\request\__init__.py
Could you check in that folder if you see a line like from .api import ... , post, ... in __init__.py, and if you go to .api if you see a function definition for post? It sounds like your requests package might have gotten gutted somehow. But the cake attribute on the requests module seems to suggest that it's definitely loading the right package, but not getting all the content for some reason.
0

Did you write import requests at the top of the file? If not, that's your problem. If yes, then the next step of debugging for me would be to do print dir(requests) since that will tell you what attributes your requests object actually has.

1 Comment

I got this for print (dir(requests)): ['__author__', '__author_email__', '__build__', '__builtins__', '__cached__', '__cake__', '__copyright__', '__description__', '__doc__', '__file__', '__license__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__title__', '__url__', '__version__']
0

Sure that HTTP_ENDPOINT and data are valid , should like below:

>>> import json

>>> url = 'https://api.github.com/some/endpoint'
>>> payload = {'some': 'data'}

>>> r = requests.post(url, data=json.dumps(payload))

Instead of encoding the dict yourself, you can also pass it directly using the json parameter (added in version 2.4.2) and it will be encoded automatically:

>>> r = requests.post(url, json=payload)

source is More complicated POST requests

2 Comments

The problem is with post method, not the data. It says AttributeError: module 'requests' has no attribute 'post'
You should got this for print(dir(requests)) :['__author__',...,'adapters', 'api', 'auth', 'certs', 'chardet', 'check_compatibility', 'codes', 'compat', 'cookies', 'delete', 'exceptions', 'get', 'head', 'hooks', 'logging', 'models', 'options', 'packages', 'patch', 'post', 'put', 'request', 'session', 'sessions', 'status_codes', 'structures', 'urllib3', 'utils', 'warnings'] can see 'post' ,i advice to remove requests lib and try to reinstall

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.