-
First check
ExampleHere's a self-contained, minimal, reproducible, example with my use case: Backend FastAPI: from fastapi import FastAPI, Form
app = FastAPI()
@app.post("/form-data")
def form_data(input: str = Form(...)):
return {"input": input}
# Returns 404 Bad RequestThe above code block is problematic for submitting HTTP POST requests from web browsers (tested on both Chrome and Safari). I am receiving a 400 Bad Request error no matter what I try. Originally I was working with uploading files, but I debugged this further and realized I have this issue with all forms in general. Here's an example of failing client code (tried with both axios and fetch to no avail): Client JS: const data = new FormData();
data.append('input', 'entry');
axios.post("/form-data", data, {
headers: {
'Authorization': 'KEY',
},
});Going deeper, I actually see this issue in my endpoint logs: Note that I am able to curl and make requests from the Python requests library just fine. This seems to be an issue specifically with requesting from the browser. Based on the log above I think there might be a problem with multipart validation. I am able to circumvent this by directly reading the request file, but this means I don't have the built-in validation for the requests: from fastapi import FastAPI, Request
app = FastAPI()
@app.post("/form-data")
def form_data(request: Request):
form = await request.form()
return {"input", form["input"]}
# Returns 200 OKI've gone through essentially every doc/post/question re: this issue, and it does not seem to be the other problems, which are:
DescriptionIn short, I am having trouble making POST HTTP requests with multipart form data directly from browsers, but I am not having this issue with curl or Python requests. Based on the symptoms, I think this issue could be due to me making the request incorrectly / FastAPI+multipart not working well with forms together (but the latter is highly unlikely). In other news, super excited to be joining the FastAPI dev community, getting my feet wet and think the tooling is awesome :) Does someone here know what's going on / could help me out? Environment
To know the FastAPI version use: python -c "import fastapi; print(fastapi.__version__)"
To know the Python version use: python --versionAdditional contextI've sank a number of hours into this already, throwing in the towel and checking here in case I'm missing something obvious. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
Have you tried setting the |
Beta Was this translation helpful? Give feedback.
-
|
Yes I have to no avail. Here are some details of the request diffs: Python Request: Axios POST: Note that both requests can succeed with a non-FastAPI debug endpoint. For reference, this is what the debug Starlette Request looks like via PyRequest: Axios POST: Sad to only have that in the block. I can only surmise something is wacky about the headers. Is there a way to figure out more debugging info? One piece of data that might help is this * appears * to only be isolated to multipart/form-data requests. Additionally making it weirder, but why I think the boundary being used might be finicky. |
Beta Was this translation helpful? Give feedback.
-
|
(Update) We discovered this issue is actually due to a proxy server on our end -- unrelated to FastAPI. From manually calling the backend it was shown that the integration indeed works. Closing this issue, thanks all for the help. |
Beta Was this translation helpful? Give feedback.
(Update) We discovered this issue is actually due to a proxy server on our end -- unrelated to FastAPI. From manually calling the backend it was shown that the integration indeed works. Closing this issue, thanks all for the help.