ad_items():
        return ORJSONResponse([{"item_id": "Foo"}])
    

Info

The parameter ` response_class ` will also be used to define the "media type"
of the response.

In this case, the HTTP header ` Content-Type ` will be set to `
application/json ` .

And it will be documented as such in OpenAPI.

Tip

The ` ORJSONResponse ` is currently only available in FastAPI, not in
Starlette.

##  HTML Response  ¶

To return a response with HTML directly from **FastAPI** , use ` HTMLResponse
` .

  * Import ` HTMLResponse ` . 
  * Pass ` HTMLResponse ` as the parameter ` response_class ` of your _path operation decorator_ . 

    
    
    from fastapi import FastAPI
    from fastapi.responses import HTMLResponse
    
    app = FastAPI()
    
    
    @app.get("/items/", response_class=HTMLResponse)
    async def read_items():
        return """
        <html>
            <head>
                <title>Some HTML in here</title>
            </head>
            <body>
                <h1>Look ma! HTML!</h1>
            </body>
        </html>
        """
    

Info

The parameter ` response_class ` will also be used to define the "media type"
of the response.

In this case, the HTTP header ` Content-Type ` will be set to ` text/html ` .

And it will be documented as such in OpenAPI.

###  Return a ` Response ` ¶

As seen in [ Return a Response directly ](../response-directly/) , you can
also override the response directly in your _path operation_ , by returning
it.

The same example from above, returning an ` HTMLResponse ` , could look like:

    
    
    from fastapi import FastAPI
    from fastapi.responses import HTMLResponse
    
    app = FastAPI()
    
    
    @app.get("/items/")
    async def read_items():
        html_content = """
        <html>
            <head>
                <title>Some HTML in here</title>
            </head>
            <body>
                <h1>Look ma! HTML!</h1>
            </body>
        </html>
        """
        return HTMLResponse(content=html_content, status_code=200)
    

Warning

A ` Response ` returned directly by your _path operation function_ won't be
documented in OpenAPI (for example, the ` Content-Type ` won't be documented)
and won't be visible in the automatic interactive docs.

Info

Of course, the actual ` Content-Type ` header, status code, etc, will come
from the ` Response ` object your returned.

###  Document in OpenAPI and override ` Response ` ¶

If you want to override the response from inside of the function but at the
same time document the "media type" in OpenAPI, you can use the `
response_class ` parameter AND return a ` Response ` object.

The ` response_class ` will then be used only to document the OpenAPI _path
operation_ , but your ` Response ` will be used as is.

####  Return an ` HTMLResponse ` directly  ¶

For example, it could be something like:

    
    
    from fastapi import FastAPI
    from fastapi.responses import HTMLResponse
    
    app = FastAPI()
    
    
    def generate_html_response():
        html_content = """
        <html>
            <head>
                <title>Some HTML in here</title>
            </head>
            <body>
                <h1>Look ma! HTML!</h1>
            </body>
        </html>
        """
        return HTMLResponse(content=html_content, status_code=200)
    
    
    @app.get("/items/", response_class=HTMLResponse)
    async def read_items():
        return generate_html_response()
    

In this example, the function ` generate_html_response() ` already generates
and returns a ` Response ` instead of returning the HTML in a ` str ` .

By returning the result of calling ` generate_html_response() ` , you are
already returning a ` Response ` that will override the default **FastAPI**
behavior.

But as you passed the ` HTMLResponse ` in the ` response_class ` too,
**FastAPI** will know how to document it in OpenAPI and the interactive docs
as HTML with ` text/html ` :

![](/img/tutorial/custom-response/image01