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.

[QUESTION] How to send 204 response?

See original GitHub issue

I tried to send 204 response in delete method

Example handler

@router.delete('/{order_id}/', tags=['smart order'], status_code=204)
async def cancel_smart_order(
        session: Session = Depends(get_session),
        order_id: UUID = Path(...)
):
    order = await session.get(order_id)

    if order.status != OrderStatus.open:
        raise HTTPException(409, f'Order have status: {order.status}')

    order.status = OrderStatus.canceled

    await session.commit_only(order)

But got error h11._util.LocalProtocolError: Too much data for declared Content-Length. Seems framefork convert None to null but set content length 0.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:27 (12 by maintainers)

github_iconTop GitHub Comments

69reactions
dmontagucommented, Feb 9, 2020

Instead of returning None, and instead of injecting the response, just return a newly created response. It would look like:

@api.get(
    "/test_request_results/{test_request_id}",
    response_model=schemas.TestRequestResults,
    responses={204: {"model": None}},
)
async def read_test_request_results(test_request_id: int):
    # ... snipped ...
    if not complete:
        return Response(status_code=HTTP_204_NO_CONTENT)

The reason you are getting content is because FastAPI uses a JSONResponse by default (instead of a Response), which converts the returned value None to "null". By returning a Response directly, you prevent FastAPI from using the JSONResponse and encoding the None.

26reactions
iacopo-papalinicommented, Sep 16, 2020

This is a bug: FastAPI is not conforming to the HTTP RFC: https://tools.ietf.org/html/rfc2616#section-10.2.5

The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields.

FastAPI should made it difficult to produce an invalid HTTP response, not allowing to create a valid one with a workaround.

Read more comments on GitHub >

github_iconTop Results From Across the Web

204 No Content - HTTP - MDN Web Docs - Mozilla
The HTTP 204 No Content success status response code indicates that a request has succeeded, but that the client doesn't need to navigate ......
Read more >
Lumen does send 204 response - php - Stack Overflow
You should do: return response('', 204);.
Read more >
HTTP Status 204 (No Content) - REST
The 204 response MUST NOT include a message-body and thus is always terminated by the first empty line after the header fields. 1....
Read more >
REST Receiver HTTP 204 response code - SAP Community
I am working on a REST scneario, where data have to be sent asynchronously to a REST service, using POST operation. In success...
Read more >
Azure usage api getting 204 response code and no content
Microsoft Q&A is the best place to get answers to all your technical questions on Microsoft products and services. Community. Forum.
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