FastAPI 0.65.2 POST request fails with "value is not a valid dict" when using the Requests library; 0.65.1 works (with a caveat)
See original GitHub issueFirst check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
- After submitting this, I commit to one of:
- Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
- I already hit the “watch” button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
- Implement a Pull Request for a confirmed bug.
First off, thank you for creating this marvellous piece of software. It is a real life-changer.
I hit a very odd bug while implementing some unit tests. Using FastAPI 0.65.2
, a POST request via the requests
module (requests.post
) consistently returns the following error:
{'detail': [{'loc': ['body'], 'msg': 'value is not a valid dict', 'type': 'type_error.dict'}]}
I created a reproducible example:
from fastapi import FastAPI
from pydantic import BaseModel
import uvicorn
app = FastAPI()
class Data(BaseModel):
field: str
@app.post("/test")
def test_data(data: Data):
return "Polo"
if __name__=='__main__':
uvicorn.run(app)
And the requests
counterpart:
import requests as rq
def test():
result = rq.post('http://127.0.0.1:8000/test', data={"field": "Marco"})
print(f"==[ result: {result.json()}")
if __name__=="__main__":
test()
The result is
==[ result: {'detail': [{'loc': ['body'], 'msg': 'value is not a valid dict', 'type': 'type_error.dict'}]}
If I downgrade FastAPI to 0.65.1
, I still get an error, but a different one:
==[ result: {'detail': [{'loc': ['body', 0], 'msg': 'Expecting value: line 1 column 1 (char 0)', 'type': 'value_error.jsondecode', 'ctx': {'msg': 'Expecting value', 'doc': 'field=heya', 'pos': 0, 'lineno': 1, 'colno': 1}}]}
This can be solved by JSONifying the dictionary:
import requests as rq
import json as js
def test():
result = rq.post('http://127.0.0.1:8000/test', data = js.dumps({"field": "Marco"}))
print(f"==[ result: {result.json()}")
if __name__=="__main__":
test()
Running the above prints
==[ result: Polo
I am slightly perplexed because I am not sure if it is the requests
library that is to blame or FastAPI. POSTing via ReDoc works without hiccups with both versions. I am at a loss as to why the second parameter to requests.post
is accepted as JSON when it should actually be a dictionary.
Please let me know if I’m missing something obvious or whether I should redirect this to the maintainers of requests
. Thank you again for making the lives of developers so much easier.
Cheers!
Issue Analytics
- State:
- Created 2 years ago
- Comments:25 (8 by maintainers)
Top GitHub Comments
You probably meant to use the json parameter, not the data parameter. Data is for form data
Can confirm, this still happens! We solved it by adding a
-H "Content-Type: application/json"
to the curl