2

I found axum::response::Redirect to redirect a user to a different page, but the example there only shows get methods. I want to know how I can do that with post. The code I'm thinking is like the following:

let app = Router::new()
    .route("/", get(crate::handlers::index::root))
    .route("/signup", post(|| async { 
        crate::handlers::signup::store_user_info_into_db,
        Redirect::to("/") 
    }));

This code doesn't work, but my idea here is that I want to call store_user_info_into_db function to store user information into a database on form submission. If the form submission was successful, then I want to redirect the user to a home page. Does anyone know how to do this?

kmdreko
  • 42,554
  • 6
  • 57
  • 106
tet
  • 1,287
  • 1
  • 17
  • 36

1 Answers1

0
async fn store_user_info_into_db() -> Redirect {
    // ...
        
    Redirect::to("/") },
}
hank
  • 1
  • 1
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Adrian Mole Sep 10 '22 at 12:49