Status code in response does not match the status_code in the decorator
See original GitHub issueFirst check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
- After submitting this, I commit to one of:
- Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
- I already hit the “watch” button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
- Implement a Pull Request for a confirmed bug. - If this is a bug, I can try raising a PR
Example
Here’s a self-contained, minimal, reproducible, example with my use case:
from typing import Optional
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from starlette import status
class MyItem(BaseModel):
item_id: str
is_offer: Optional[bool] = None
app = FastAPI()
@app.post("/items", status_code=status.HTTP_201_CREATED)
async def post_item(item: MyItem):
return JSONResponse({"item_id": item.item_id})
Description
New user of FastAPI here. I have considerable experience with flask where return codes are set in the return statement. Took an example from the documentation and gave it a run to test my understanding. Swagger Docs respond correctly.
- POST a request to the endpoint
/items
(default example in swagger). - It returns with status code
200
. - But I expected it to return
201
.
Is this expected? Have I misunderstood something in the docs?
My understanding of the status_code parameter is that it is the default status code which can be overridden by <Response>.status_code = <int>
Is status_code
just for the Swagger docs?
Environment
- OS: [e.g. Linux / Windows / macOS]: Ubuntu
- FastAPI Version [e.g. 0.3.0]: 0.61.1 (even tried with 0.60.0 to check if it is just happening for this version)
- Python Version: 3.7.5
Additional context
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
how to send error codes in nestjs app from controller?
i tried to inject response object in a method but there is no method to send error. save(@Body() body: any, @Res() response: Response):...
Read more >Status codes in HTTP - W3C
Server has received the request but there is no information to send back, and the client should stay in the same document view....
Read more >Troubleshoot Azure Cosmos DB bad request exceptions
A response with this error means you are executing an operation and passing a partition key value that does not match the document's...
Read more >HTTP response status codes - MDN Web Docs - Mozilla
This response code means the returned metadata is not exactly the same as is available from the origin server, but is collected from...
Read more >Describing Responses - Swagger
A response is defined by its HTTP status code and the data returned in ... JSON is the most common format for data...
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 FreeTop 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
Top GitHub Comments
Yep, exactly what @Kludex said. 👆
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.