[Python] How to add several cookies to response in FastHTML?

Discussão em 'Python' iniciado por Stack, Fevereiro 11, 2025.

  1. Stack

    Stack Membro Participativo

    There are similar questions (Add several cookies in IHTTPResponse and Setting several cookies in same response) but they are not about FastHTML.

    I want to add 2 or more cookies to the response (cookie1 and cookie2 in the example)

    from fasthtml import common as fh
    @app.get("/route1")
    def getroute1(request):
    return fh.P("This is a test"), fh.cookie('cookie1', 'value1'), fh.cookie('cookie2', 'value2')


    Only the last cookie (cookie2) is stored, even if I add more cookies.

    I have also tried this return sentence, with the same result (only the last cookie is stored)

    return fh.P("This is a test"),
    fh.HttpHeader(k='set-cookie', v='cookie1=value1; Path=/; SameSite=lax'),
    fh.HttpHeader(k='set-cookie', v='cookie2=value2; Path=/; SameSite=lax')

    Continue reading...

Compartilhe esta Página