Hi @firebits.io, @jofas and @kornel,
Thank you for your helps, discussions and suggestions.
-- I appreciate it. This makes my learning journey less scary 
Hi @jofas,
Thank you for the suggestion. I have tested this, and it appears to work for what I would like to do.
Thank you and best regards,
...behai.
For completeness, I have followed up @jofas suggestion:
And this is what I've working:
Refactored version of some_route_page(...) which consumes the cookie if set:
#[get("/some/route")]
pub async fn some_route_page(
request: HttpRequest
) -> impl Responder {
match request.cookie("redirect-message") {
None => {},
Some(c) => println!("some_route_page() redirect-message [{}]", c.value())
}
let mut cookie = build_redirect_cookie(request, "redirect-message", "");
cookie.make_removal();
HttpResponse::Ok()
.content_type(ContentType::html())
.cookie(cookie)
.body("using tera crate to load HTML from disk.")
}
New helper function build_redirect_cookie(...):
fn build_redirect_cookie<'a>(
request: HttpRequest,
name: &'a str,
value: &'a str
) -> Cookie<'a> {
// Header "host" should always be in the request headers.
let host = request.headers().get("host").unwrap().to_str().unwrap().to_owned();
// Remove the port if any.
let parts = host.split(":");
Cookie::build(name, value)
.domain(String::from(parts.collect::<Vec<&str>>()[0]))
.path("/")
.secure(true)
.http_only(true)
.finish()
}
Extracting the domain out of host header looks scary for me. I'll refactor it into a helper method later.
Server redirect code:
...
let cookie= build_redirect_cookie(request, "redirect-message", "This page was redirected...");
HttpResponse::Ok()
.status(StatusCode::SEE_OTHER)
.append_header((header::LOCATION, "/some/route"))
.cookie(cookie)
.finish()
StatusCode::SEE_OTHER should be used in this case, because the original user request is a POST, and the server redirects to a GET route.
Thank you again and best regards,
...behai.