Skip to content

How to set cookie in response? #351

Answered by davidpdrsn
goldwind-ting asked this question in Q&A
Discussion options

You must be logged in to vote

There are many ways to add headers to a response. See here for examples.

The easiest way is to use axum::response::Headers:

use axum::{
    http::header::SET_COOKIE,
    response::{Headers, Html, IntoResponse},
};

async fn handler() -> impl IntoResponse {
    let headers = Headers([(SET_COOKIE, "key=value")]);
    let content = Html("<h1>Hello, World!</h1>");
    (headers, content)
}

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@goldwind-ting
Comment options

@k7575
Comment options

@davidpdrsn
Comment options

@merlindru
Comment options

Answer selected by davidpdrsn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants
Converted from issue

This discussion was converted from issue #350 on September 28, 2021 09:54.