question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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 issue

First 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:closed
  • Created 2 years ago
  • Comments:25 (8 by maintainers)

github_iconTop GitHub Comments

61reactions
Mausecommented, Jun 15, 2021

You probably meant to use the json parameter, not the data parameter. Data is for form data

55reactions
thefirebankscommented, Jul 26, 2021

Can confirm, this still happens! We solved it by adding a -H "Content-Type: application/json" to the curl

Read more comments on GitHub >

github_iconTop Results From Across the Web

GET request results in typeerror (value is not a valid dict) - ...
Why does FASTApi expect a dictionary here? I don´t really understand it, since I am not able to even print the response. How...
Read more >
Fastapi Value Is Not A Valid Dict
I am sending . FastAPI 0.65.2 POST request fails with value is not a valid dict when using the Requests library; 0.65.1 works...
Read more >
tiangolo/fastapi - Gitter
I've been trying to create a decorator that can decorate both async and normal functions. I'm a bit stuck. This decorator @duration works...
Read more >
value is not a valid dict fastapi error with solution - YouTube
In this video you'll get solution for the error value is not a valid dict when you are passing data to fastapi from...
Read more >
Using Path, File and Form returns -> msg": "value is not a ...
I have an endpoint configured as such -> @app.post("/detect/{model_uuid}", summary="Run detection on an image") async def object_detection(…
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found