Replies: 2 comments
-
|
You can do this by implementing struct OddNumber(u32);
impl<'a> FromParam<'a> for OddNumber {
type Error = &'static str;
fn from_param(param: &'a str) -> Result<Self, Self::Error> {
if let Ok(number) = param.parse::<u32>() {
if number % 2 == 1 {
Ok(Self(number))
} else {
Err("Even Number")
}
} else {
Err("Not a valid number")
}
}
}
#[get("/number/<num>", rank = 1)]
fn odd_number(num: OddNumber) -> Option<content::RawHtml<String>> {
let html = format!("Odd number: {:?}", num.0);
return Some(content::RawHtml(html));
}
#[get("/number/<num>", rank = 2)]
fn any_number(num: u32) -> content::RawHtml<String> {
let html = format!("Any number: {num:?}");
return content::RawHtml(html);
}If the |
Beta Was this translation helpful? Give feedback.
-
|
A more general answer - you can't forward a request once a handler has executed. Rocket doesn't allow more than one handler to run for a given request. More specifically, Rocket can only run at most one data guard, since Rocket uses a streaming data model for the request body. @watsom27's answer would help for your specific situation listed here, but I this may not always be possible. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I was hoping to be able to forward a request from within a route by returning a
None- not a guard. I tried this:Unfortunately I get a
404 Not Foundfor the 42 and theany_numberfunctions is not called.Did I make a mistake in the example or is there some other way to do this from within the route?
Beta Was this translation helpful? Give feedback.
All reactions