0
0
FastAPIframework~5 mins

Custom response classes in FastAPI

Choose your learning style9 modes available
Introduction

Custom response classes let you control how your FastAPI app sends data back to users. This helps you change the format, headers, or style of the response easily.

When you want to send plain text instead of JSON.
When you need to return HTML pages from your API.
When you want to send files like images or PDFs.
When you want to customize HTTP headers in the response.
When you want to change the response status code or media type.
Syntax
FastAPI
from fastapi import FastAPI

app = FastAPI()

@app.get("/path", response_class=ResponseClassName)
def function_name():
    return ResponseClassName(content, media_type="type")

Replace ResponseClassName with the FastAPI response class you want, like PlainTextResponse or HTMLResponse.

The content is the data you want to send back, and media_type tells the browser how to handle it.

Examples
This example sends plain text instead of JSON.
FastAPI
from fastapi import FastAPI
from fastapi.responses import PlainTextResponse

app = FastAPI()

@app.get("/text", response_class=PlainTextResponse)
def get_text():
    return "Hello, this is plain text!"
This example returns HTML content that browsers can render.
FastAPI
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()

@app.get("/html", response_class=HTMLResponse)
def get_html():
    return "<h1>Welcome to FastAPI!</h1>"
This example shows how to customize JSON response with a different status code.
FastAPI
from fastapi import FastAPI
from fastapi.responses import JSONResponse

app = FastAPI()

@app.get("/custom-json", response_class=JSONResponse)
def get_custom_json():
    return JSONResponse(content={"message": "Custom JSON response"}, status_code=202)
Sample Program

This FastAPI app has two routes. One returns a full HTML page, and the other returns plain text. Both use custom response classes to tell FastAPI how to send the data.

FastAPI
from fastapi import FastAPI
from fastapi.responses import PlainTextResponse, HTMLResponse

app = FastAPI()

@app.get("/welcome", response_class=HTMLResponse)
def welcome():
    html_content = """
    <html>
        <head>
            <title>Welcome Page</title>
        </head>
        <body>
            <h1>Hello, visitor!</h1>
            <p>This is a custom HTML response.</p>
        </body>
    </html>
    """
    return html_content

@app.get("/message", response_class=PlainTextResponse)
def message():
    return "This is a plain text message."
OutputSuccess
Important Notes

Using custom response classes helps browsers and clients understand the data format better.

You can create your own response classes by extending FastAPI's Response class if needed.

Always set the correct media_type to avoid confusion in clients.

Summary

Custom response classes control how FastAPI sends data back.

Use them to send text, HTML, files, or custom JSON easily.

They help make your API responses clear and correct for users and browsers.