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.

[FEATURE] Support repeated key=value in form data

See original GitHub issue

Is your feature request related to a problem

Yes.

Given some URL encoded data like this…

choices=parrot&choices=spider

…only the last key=value wins.

This does not work like I expected:

choices: list = Form(...)

You can only validate against the last value.

The solution you would like

Perhaps FastAPI should collect repeated keys in the 2-tuple list that request.form() gives and assign those values as a list to the same key before validation happens.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
HitLucacommented, Jun 9, 2021

I think we still have an issue with Form and List. When passing a list of elements as a Form field, it doesn’t get parsed correctly:

@app.put("/test1")
def test1(values: List[str] = Form(...)) -> None:
    return values


@app.put("/test2")
def test2(values: List[str] = Query(...)) -> None:
    return values

test1 example

curl -X 'PUT' \
  'http://localhost:5050/test1' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'values=string,string'
[
  "string,string"
]

test2 example

curl -X 'PUT' \
  'http://localhost:5050/test2?values=string&values=string' \
  -H 'accept: application/json'
[
  "string",
  "string"
]

we can see how Query correctly parses the list, whereas Form doesn’t. Changing List[str] to list doesn’t fix the problem. Accepting list of numbers with List[int] for example returns an error as shown below, indicating that typing.List isn’t handled as expected

@app.put("/test3")
def test3(values: List[int] = Form(...)) -> None:
    print(type(values), values)
    return values

test3 example

curl -X 'PUT' \
  'http://localhost:5050/test3' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'values=1,2'
{
  "detail": [
    {
      "loc": [
        "body",
        "values",
        0
      ],
      "msg": "value is not a valid integer",
      "type": "type_error.integer"
    }
  ]
}

The curl commands have been copy pasted from the swagger web interface but I don’t see any change when running them from terminal. Using fastapi==0.65.2

1reaction
tiangolocommented, Jan 18, 2020

Thanks @StephenCarboni for the report 🔍 🕵️‍♂️

Thanks @nsidnev for the fix! 🎉 🚀

And thanks everyone here for the discussion.

The fix is available in the latest release.

Read more comments on GitHub >

github_iconTop Results From Across the Web

FormData.entries() - Web APIs | MDN
The FormData.entries() method returns an iterator which iterates through all key/value pairs contained in the FormData .
Read more >
How to get multiple key values pair under array of object ...
How to get multiple key values pair under array of object through form data submition · You can do something like . var...
Read more >
How can I send a list of records (multiple forms with key-value ...
How can I send a list of records (multiple forms with key-value pairs) in a single request via multipart/form-data HTTP POST.
Read more >
FormData - The Modern JavaScript Tutorial
FormData objects can help with that. As you might have guessed, it's the object to represent HTML form data. The constructor is:.
Read more >
Prevent duplicate values in a table field using an index
Set the field's Indexed property to Yes (No duplicates) You can do this by opening the table in Design view. · Create a...
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