Beginner struggling with form POST using actix_web

⚓ Rust    📅 2025-11-23    👤 surdeus    👁️ 9      

surdeus

Hello,

I am relearning Rust and programming a bit, so I am not sure what I am doing wrong and my troubleshooting has just brought me in circles.

I haven't seen a lot of examples using actix_web that also include front end code, so I'm not sure if I'm just not structuring things properly on the front end, if I'm failing on the back end, or both.

Any help would be appreciated.

The basic webpage:

<form action="/form" id="form" method="POST">
    <div>
        <label for="username">Username:</label>
        <input name="username" id="username" />
    </div>
    <div>
        <label for="test">Test:</label>
        <input name="test" id="test" />
    </div>
    <div>
        <button id="submitButton">Submit</button>
    </div>
</form>
</body>
<script>
    document.addEventListener('DOMContentLoaded', function() {
        const send = document.querySelector('form')

        send.addEventListener("submit", async (e) => {
            e.preventDefault();
            var formData = new FormData(document.querySelector("#form"));
            for (var [key, value] of formData.entries()) {
                console.log(key, value);
            }
            const response = await fetch("/form", {method: "POST", body: formData });
            console.log(await response.text());
        });
    });
</script>

Snippets of the rust code:

#[derive(Deserialize)]
struct Info {
    username: String,
    test: String,
}
#[post("/form")]
async fn form(form: web::Json<Info>) -> HttpResponse {
    HttpResponse::Ok().body(format!("Welcome from the form {} with test: {}!", form.username, form.test))
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        //let cors = Cors::default().allow_any_origin().send_wildcard();
        //App::new().wrap(cors)
        App::new()
            .service(fs::Files::new("/", "./static").show_files_listing())
            .service(form)
    })
        .bind(("127.0.0.1", 3000))?
        .run()
        .await
}

My attempt at sending a request with curl. I have gotten GET to work easily on simple examples but not POST with a form, or GET with a form, which seems like it would require destructuring the form inputs manually.

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"username":"xyz","test":"xyz"}' \
  http://127.0.0.1:3000/form
Request did not meet this resource's requirements.%   

4 posts - 2 participants

Read full topic

🏷️ Rust_feed