I am currently using an implementation of FromRequest to access Query Data from the Request URI property and it seems to be working fine for GET Methods but does not work for POST Methods.
I have a very simple Rocket (0.5.0-rc.2) Script that is only setting up a Server with an implementation of the FromRequest, then the two Routes I am using to show an example of what is happening.
use rocket;
use rocket::{Config, Rocket, FromForm};
use rocket::request::{FromRequest, Request, Outcome};
use rocket::http::uri::Query;
#[tokio::main]
async fn main() {
let figment = Config::figment().merge(("address", "0.0.0.0")).merge(("port", 3000));
let _rocket = Rocket::build()
.configure(figment)
.mount("/get", rocket::routes![test_get])
.mount("/post", rocket::routes![test_post])
.launch().await.expect("ERROR");
}
#[derive(FromForm)]
pub struct QueryParams { data:String }
#[rocket::async_trait]
impl<'r> FromRequest<'r> for QueryParams {
type Error = rocket::Error;
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
let query:Query = request.uri().query().unwrap();
let string:String = String::from(query.to_string());
Outcome::Success(QueryParams { data:String::from(string) })
}
}
#[rocket::get("/")]
fn test_get(query_params:QueryParams) -> &'static str {
println!("{:?}", query_params.data);
return "message : Get Status OK";
}
#[rocket::post("/")]
fn test_post(query_params:QueryParams) -> &'static str {
println!("{:?}", query_params.data);
return "message : Post Status OK";
}
Inside the implement FromRequest it is getting the Query Object from request.uri().query().unwrap()
but it is always None for POST, when I output the request.uri()
I am getting the following
println!("{:?}", request.uri());
For GET
Origin { source: None, path: Data { value: "/get", decoded_segments: ["get"] }, query: Some(Data { value: "key01=val01", decoded_segments: [uninitialized storage] }) }
For POST
Origin { source: None, path: Data { value: "/post", decoded_segments: ["post"] }, query: None }
How can POST work with data from request.uri().query()
?