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.

Status code in response does not match the status_code in the decorator

See original GitHub issue

First 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

image

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
tiangolocommented, Oct 21, 2022

Yep, exactly what @Kludex said. 👆

Sorry for the long delay! 🙈 I wanted to personally address each issue and they piled up through time, but now I’m checking each one in order.

0reactions
github-actions[bot]commented, Nov 1, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

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