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.

Error 422: Unprocessable entity when I send form data from JS script

See original GitHub issue

Backend

from fastapi import FastAPI, File, Form, UploadFile, Request
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()

origins = ["http://127.0.0.1:5500"]
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


@app.post("/test")
def test(username: str = Form(...)):
    print(f"########### Received {username} #############")

Frontend

async function sendData() {
    formData = new FormData();
    formData.append('username', 'Kareem');
    const response = await fetch('http://127.0.0.1:8000/test', {
        method: 'POST',
        data: formData
    })
    console.log(response)

Response

Response {
body: (...)
bodyUsed: false
headers: Headers {}
ok: false
redirected: false
status: 422
statusText: "Unprocessable Entity"
type: "cors"
url: "http://127.0.0.1:8000/test"
__proto__: Response }

Description

  • The /test endpoint is simply supposed to recieve a form containing only username and print it out.
  • When I test the API using swagger docs it works fine as expected, but when I try to send form data from an external script it throws me this error and that response object above.

Environment

  • OS: Linux
  • FastAPI version: 0.61.1
  • Python version: 3.8.2

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:20 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
Mausecommented, Nov 17, 2020

Click on the three little dots next to body?

1reaction
dash-alex-berndtcommented, Aug 10, 2021

Has anyone figured out what the issue was? I have a frontend which must use .fetch() so cannot use XMLHttpRequest.

Read more comments on GitHub >

github_iconTop Results From Across the Web

FastAPI returns "Error 422: Unprocessable entity" when I send ...
The 422 response body will contain an error message about which field(s) is missing or doesn't match the expected format.
Read more >
422 Unprocessable Entity - HTTP - MDN Web Docs
The HyperText Transfer Protocol (HTTP) 422 Unprocessable Entity response status code indicates that the server understands the content type ...
Read more >
422 Unprocessable Entity on updating record with image
Hello guys, newbee here. I want to update a record with image in the modal but when I clicked the update I have...
Read more >
Solved: Re: 422 Error when making POST Request to Favorite...
I can get the list of a user's favorites without any issues, but I am getting a 422 "unprocessable entity" response when I...
Read more >
422 Unprocessable Entity Error. I tried upload Excel file to ...
[Solved]-422 Unprocessable Entity Error. I tried upload Excel file to import data but it is not working-Vue.js ... Adding file_date in form: new...
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