Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using pydantic Json Type as Form data type doesn't work #10997

Open
9 tasks done
Kludex opened this issue Jan 21, 2024 Discussed in #9305 · 1 comment
Open
9 tasks done

Using pydantic Json Type as Form data type doesn't work #10997

Kludex opened this issue Jan 21, 2024 Discussed in #9305 · 1 comment
Labels
question Question or problem

Comments

@Kludex
Copy link
Sponsor Collaborator

Kludex commented Jan 21, 2024

Discussed in #9305

Originally posted by harpaj March 23, 2023

First Check

  • I added a very descriptive title here.
  • I used the GitHub search to find a similar question 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.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from typing import Annotated

from fastapi import FastAPI, Form
from pydantic import Json, BaseModel

app = FastAPI()


class JsonListModel(BaseModel):
    json_list: Json[list[str]]


@app.post("/working")
async def working(json_list: Annotated[str, Form()]) -> list[str]:
    model = JsonListModel(json_list=json_list)
    return model.json_list


@app.post("/broken")
async def broken(json_list: Annotated[Json[list[str]], Form()] ) -> list[str]:
    return json_list

Description

In the example code above, I would expect working and broken to be approximately equivalent.
However, while working returns the parsed json_list as expected, broken fails with

{"detail":[{"loc":["body","json_list"],"msg":"JSON object must be str, bytes or bytearray","type":"type_error.json"}]}

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.95.0

Python Version

Python 3.10.8

Additional Context

sample request that fails with broken and works with working:

import requests
import json

headers = {"Content-Type": "application/x-www-form-urlencoded"}
response = requests.post(
    "http://0.0.0.0:8000/broken",
    data={"json_list": json.dumps(["abc", "def"])},
    headers=headers,
)
```</div>
@Kludex Kludex added the question Question or problem label Jan 21, 2024
@dayeondev
Copy link

Hi @Kludex @harpaj
As I've been reviewing this issue, a few questions have arisen.

  1. Is the primary goal of this issue to enable the Form() function to directly receive data in JSON format? Or, is the focus more on providing documentation and examples for handling this indirectly?
  2. Is there a need for detailed documentation on this phenomenon and its causes? Is the goal to provide guidelines to help users understand and appropriately manage this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem
Projects
None yet
Development

No branches or pull requests

2 participants