1

I'm a beginner to rust and to the rocket framework. I am building a simple login application which takes in details of the user and stores it in the database. But I can't seem to figure out how to receive the data from the HTML form.

I saw: https://rocket.rs/v0.5-rc/guide/requests/#forms as per which I coded this. My code: main.rs

use rocket::*;
use rocket::response::content::RawHtml;
use rocket::http::RawStr;
use rocket::form::Form;

#[get("/")]
fn index() -> RawHtml<&'static str> {
  RawHtml(include_str!("templates/index.html"))
}

#[derive(FromForm)]
struct UserInput<'r> {
    r#type: &'r str,
}

// #[post("/create_account", data = "<userdata>")]
// fn create_account(user_input: Form<UserInput>) -> RawHtml<&'static str> {
//   print(format!("Your value: {}", user_input.value));
//   return RawHtml(include_str!("templates/account.html", messages = messages))
// }

//use rocket::form::Form;

#[derive(FromForm)]
#[derive(Debug)]
struct user_input<'r> {
    r#type: &'r str,
}


#[post("/create_account", data = "<task>")]
fn create_account(task: Form<user_input<'_>>) -> RawHtml<&'static str> { 
  println!("{:#?}",task);
  return RawHtml(include_str!("templates/account.html"))
}

#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
  sql::setup();
    let _rocket = rocket::build()
        .mount("/", routes![index])
        .mount("/", routes![create_account])
        // .mount("/confirmation", routes![confirmation])
        .launch()
        .await?;
    Ok(())
}

HTML form file:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>Account Creation</h1>
    <br>
    <h2>User Details</h2>
    <form action="/create_account" method="post">
        <!-- name, age, email, phno, password -->
        <label for="Name">Name (Enter Full Name)</label>
        <br>
        <input type="text" name="Name" id="Name" text="Name">
        <br>
        <label for="age">Age</label>
        <br>
        <input type="number" name="age" id="age">
        <br>
        <label for="email">BCBS outlook email</label>
        <br>
        <input type="email" name="email" id="email" text="BCBS outlook email">
        <br>
        <label for="phno">Phone Number</label>
        <br>
        <input type="tel" name="phno" id="phno">
        <br>
        <label for="password">Password</label>
        <br>
        <input type="password" name="password" id="password">
        <br> <br>
        <input type="submit" value="Create Account">
    </form>
</body>
</html>

Thanks in advance

1 Answers1

1

Use lowercase names in your input elements.

    <input type="text" name="name" id="name">

You can then use those fields in your rust code.

#[derive(FromForm)]
struct CreateAccount {
    name: String,
}

#[post("/create_account", data = "<account>")]
fn create_account(account: Form<CreateAccount>) -> RawHtml<&'static str> {
    println!("{:#?}",account.name);
    return RawHtml(include_str!("templates/account.html"))
}
AlexN
  • 1,613
  • 8
  • 21