When attempting to send a POST request from my single page application, I encounter a 404 error, even though the route functions properly in Postman.
routes.rs
#[post("/letters", format = "application/json", data = "<new_letter>")]
fn write_letter(new_letter: Json<NewLetter>, conn: DbConn) -> Json<Value> {
Json(json!({
"status": Letter::write(new_letter.into_inner(), &conn),
"result": null
}))
}
I have configured my main.rs file to handle CORS
let (allowed_origins, failed_origins) = AllowedOrigins::some(&["http://localhost:3000"]);
let options = rocket_cors::Cors {
allowed_origins: allowed_origins,
allowed_methods: vec![Method::Get, Method::Put, Method::Post, Method::Delete]
.into_iter()
.map(From::from)
.collect(),
allowed_headers: AllowedHeaders::all(),
allow_credentials: true,
..Default::default()
};
All routes function correctly in Postman, and GET requests work from the application. However, a 404 error is encountered when attempting a POST request from the application. The backend logs display the following error:
OPTIONS /api/letters:
=> Error: No matching routes for OPTIONS /api/letters.
=> Warning: Responding with 404 Not Found catcher.
=> CORS Fairing: Turned missing route OPTIONS /api/letters into an OPTIONS pre-flight request
=> Response succeeded.
Here is the relevant front end code:
writeLetter: (letter) => axios.post(`${base_url}/api/letters`, letter)
.then(res => {
if (res.status == 201) {
console.log("letter successfully submitted")
return res
}
throw new Error(res.error)
}),
Is the issue with my Axios implementation or rocket_cors setup? I came across a similar issue but it seems like I have configured it correctly.