Skip to content

Sending (HX-Trigger) headers in response #46

Description

@hf-kklein

In plain FastAPI I can add headers to my response as described here

def get_headers(response: Response):
    response.headers["X-Cat-Dog"] = "alone in the world"
    return {"foo": "bar"} # this is fed to the jinja2 template

But this doesn't work when using an @htmx decorated endpoint.
The response and its headers are ignored and overwritten with the Template response.

Seems like a bug or at least missing feature to me.

Activity

  1. hf-kklein commented on Apr 2, 2024

    @hf-kklein
    Author

    For anyone having the same issue:

    from functools import wraps
    from typing import Awaitable, Callable
    
    from fastapi import Response
    
    _DecoratedFuncType = Callable[..., Awaitable[Response]]
    
    
    def add_hx_trigger_header_on_success(header_value: str) -> Callable[[_DecoratedFuncType], _DecoratedFuncType]:
        """
        Adds the 'HX-Trigger' header to the response if the response status code is a success code.
        See https://htmx.org/attributes/hx-trigger/#triggering-via-the-hx-trigger-header and
        https://htmx.org/headers/hx-trigger/ for details on this header.
        This is a workaround for https://github.com/maces/fastapi-htmx/issues/46
        :param header_value: the value to set for the 'HX-Trigger' header
        """
    
        def decorator(func: _DecoratedFuncType) -> _DecoratedFuncType:
            @wraps(func)
            async def wrapper(*args, **kwargs):
                response = await func(*args, **kwargs)
                if isinstance(response, Response):
                    is_success_status_code = 200 <= response.status_code < 300
                    if is_success_status_code:
                        response.headers.append("HX-Trigger", header_value)
    
                return response
    
            return wrapper
    
        return decorator

    and then use it like:

    @router.post("/foo", response_class=HTMLResponse, status_code=status.HTTP_201_CREATED)
    @add_hx_trigger_header_on_success('my-magic-event')
    @htmx("my-template-path", "index")
    async def create_foo_htmx(
        request: Request
    ):
        ...

    and

        <anHtmlTag hx-get="..." hx-trigger="load, my-magic-event from:body">
  2. maces commented on Apr 4, 2024

    @maces
    Owner

    Thanks for the workaround. Linking #31 for reference. Will keep this open until it is supported officially.

  3. self-assigned this
    on Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions