Error 422: Unprocessable entity when I send form data from JS script
See original GitHub issueBackend
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 onlyusername
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:
- Created 3 years ago
- Comments:20 (9 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Click on the three little dots next to body?
Has anyone figured out what the issue was? I have a frontend which must use .fetch() so cannot use XMLHttpRequest.