I am trying to send a post request to a python backend running on a fast API server. The current method I am using below throws error test() takes 0 positional arguments but 1 was given
but I haven't passed any arguments to the function test
. Why is this happening and how can I fix it? Thanks in advance.
import requests
data = {"foo": "bar"}
r = requests.post("http://127.0.0.1:8000/test", json = data)
print(r.text)
import requests
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.route('/test', methods=['POST'])
def test(request):
print(request.form['foo'])
return 'thisIsResponse'
EDIT: as per John Gordon's answer I updated my code by assigning a positional argument to the test function now I get error TypeError: 'method' object is not subscriptable
Stack trace:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/uvicorn/protocols/http/httptools_impl.py", line 375, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/usr/lib/python3/dist-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__
return await self.app(scope, receive, send)
File "/home/spree/.local/lib/python3.10/site-packages/fastapi/applications.py", line 271, in __call__
await super().__call__(scope, receive, send)
File "/home/spree/.local/lib/python3.10/site-packages/starlette/applications.py", line 118, in __call__
await self.middleware_stack(scope, receive, send)
File "/home/spree/.local/lib/python3.10/site-packages/starlette/middleware/errors.py", line 184, in __call__
raise exc
File "/home/spree/.local/lib/python3.10/site-packages/starlette/middleware/errors.py", line 162, in __call__
await self.app(scope, receive, _send)
File "/home/spree/.local/lib/python3.10/site-packages/starlette/middleware/cors.py", line 84, in __call__
await self.app(scope, receive, send)
File "/home/spree/.local/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
raise exc
File "/home/spree/.local/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
await self.app(scope, receive, sender)
File "/home/spree/.local/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
raise e
File "/home/spree/.local/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
await self.app(scope, receive, send)
File "/home/spree/.local/lib/python3.10/site-packages/starlette/routing.py", line 706, in __call__
await route.handle(scope, receive, send)
File "/home/spree/.local/lib/python3.10/site-packages/starlette/routing.py", line 276, in handle
await self.app(scope, receive, send)
File "/home/spree/.local/lib/python3.10/site-packages/starlette/routing.py", line 66, in app
response = await func(request)
File "/home/spree/spreeTv/./ods_info.py", line 30, in test
print(request.form['foo'])
TypeError: 'method' object is not subscriptable