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 can I remove the media type from the main response

See original GitHub issue

Description

How can I remove the media type from the main response?

@router.delete("/topics/{topic}",
               summary="Delete topic",
               response_class=None,
               responses={
                   200: {"description": "Topic successfully deleted"},
                   404: {"description": "Topic not found"}
               })
async def delete_topic(topic):
    """
    Delete a topic with all of it's entries
    """
    deleted = await delete_topic(topic)
    if not deleted:
        return Response(status_code=404)

    return

In this example the open-api documentation will show no media type for the 404 response, but it will show application/json for the 200 response. How can I remove that. My response body is always empty.

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
mdbugcommented, Oct 30, 2019

Thank you for your answer. But if I do it like this

@router.delete("/topics/{topic}",
               summary="Delete topic",
               response_class=None,
               responses={
                   200: {"description": "Topic successfully deleted"},
                   404: {"description": "Topic not found"}
               })
async def delete_topic(topic, redis: Redis = Depends(get_redis)):
    """
    Delete a topic with all of it's entries
    """
    deleted = await redis.delete(get_topic_stream_key(topic))
    if deleted == 0:
        return Response(status_code=404)

    return Response(status_code=200)

the documentaion still shows response

And if I change response_class to response_class=Response I get “AssertionError: A response class with media_type is needed to generate OpenAPI”

4reactions
tiangolocommented, Apr 12, 2020

You now can use response_class=Response, I’m not sure since which version 🤷 (we are now on version 0.54.1).

But this seems to work as you expected:

from fastapi import FastAPI, Response

app = FastAPI()


@app.delete(
    "/topics/{topic}",
    summary="Delete topic",
    response_class=Response,
    responses={
        200: {"description": "Topic successfully deleted"},
        404: {"description": "Topic not found"},
    },
)
async def delete_topic(topic: str):
    """
    Delete a topic with all of it's entries
    """
    return Response(status_code=200)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Remove a media type from a controller method - Stack Overflow
1 Answer 1 ... You could use a ResultFilterAttribute and override the OnResultExecuted method which is triggered right after the action gets ...
Read more >
Media Types - Swagger
Media type is a format of a request or response body data. Web service operations can accept and return data in different formats,...
Read more >
MIME types (IANA media types) - HTTP - MDN Web Docs
MIME types are case-insensitive but are traditionally written in lowercase. The parameter values can be case-sensitive.
Read more >
ASP.NET Web API: Media-Type Formatters - TutorialsTeacher
Media type formatters are the classes responsible for serializing request/response data so that Web API can understand the request data format and send...
Read more >
Additional Responses in OpenAPI - FastAPI - tiangolo
You can use this same responses parameter to add different media types for the same main response. For example, you can add an...
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