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.

`List[...]` as type for a `Form` field doesn't work as expected

See original GitHub issue

Opening a new issue since no action has been taken on https://github.com/tiangolo/fastapi/issues/842 for more than a month.

Looks like we have an issue on how fastapi handles lists of elements passed as Form parameter (so anything that is type hinted List[...] = Form(...)). The issue doesn’t arise when they are defined as Query parameters. After some digging I managed to get to the root of the problem, which is how we pass the parameters when making an api call. fastapi indeed correctly understands that the input needs to be a list of elements, but doesn’t parse the list if it comes from a single field.

Example using Query, works as expected

@app.post("/")
def api_test(l: List[int] = Query(...)) -> List[int]:
    return l

Api request generated using the swagger UI

curl -X 'GET' \
  'http://localhost:5009/?l=1&l=2' \
  -H 'accept: application/json'

note how the passed values are l=1&l=2

[
  1,
  2
]

Example using Form, doesn’t work as expected

@app.post("/")
def api_test(l: List[int] = Form(...)) -> List[int]:
    return l

Api request generated using the swagger UI

curl -X 'POST' \
  'http://localhost:5009/' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'l=1,2'

note how the passed values are l=1,2

{
  "detail": [
    {
      "loc": [
        "body",
        "l",
        0
      ],
      "msg": "value is not a valid integer",
      "type": "type_error.integer"
    }
  ]
}

Manual api request

curl -X 'POST' \
  'http://localhost:5009/' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'l=1' \
  -d 'l=2'

Now the values are passed as -d 'l=1' and -d 'l=2'

[
  1,
  2
]

I am fairly sure that l=1,2 should be an accepted way of passing a list of values as parameter, but fastapi doesn’t seem to like it. Also, if this isn’t the case, the Swagger UI doesn’t produce the correct curl requests for lists given as Form parameters.

Packages:

  • fastapi: 0.66.0
  • python: 3.7.4

Let me know if you need other details!

Thank you for coming to my TED talk

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:13
  • Comments:17 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
phillipuniversecommented, Nov 17, 2022

Then why does the Swagger UI produce curl requests which clearly don’t work?

You’ve got me! I don’t know what is causing it. 😢

~@HitLuca @uricholiveira probably the miss here is that FastAPI/Pydantic need to use the explode keyword in the OpenAPI specification. It looks like the default is simple in the OpenAPI spec which yields the CSV form.~

EDIT - this is a bug in swagger-ui see https://github.com/swagger-api/swagger-js/issues/2713

That linked doc talks about query parameters but there is a mention of it when describing request bodies too. Looks like the same rules apply.

1reaction
falkbencommented, Nov 18, 2022

This worked perfectly @phillipuniverse!

I modified it slightly to use the walrus operator (my first time using the walrus operator, in fact)

    if form_patch := (
        content.get("application/x-www-form-urlencoded")
        or content.get("multipart/form-data")
    ):
Read more comments on GitHub >

github_iconTop Results From Across the Web

Pressing Enter key on input field does not work as expected
I tried changing the type to = submit and to button, but the behavior doesn't change. What can I do to make sure...
Read more >
<input>: The Input (Form Input) element - HTML
The <input> HTML element is used to create interactive controls for web-based forms in order to accept data from the user; a wide...
Read more >
Proposed Error message describes invalid form field value
Open in a new tab. This input element does not have a form field error indicator. <form> <label for="filter">Product filter</label> <input type="text" ...
Read more >
Chapter 18 Forms and Form Fields
This book won't try to comprehensively discuss all field types, but we will start with a rough overview. A lot of field types...
Read more >
HTML Input Attributes
The value of a disabled input field will not be sent when submitting the form! ... Note: The size attribute works with the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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