How can I set the HTTP status code of a (Rust) Rocket API endpoint's Template response?

白昼怎懂夜的黑 提交于 2021-01-27 17:24:54

问题


I have the following login POST endpoint handler in my Rocket API:

#[post("/login", data = "<login_form>")]
pub fn login_validate(login_form: Form<LoginForm>) -> Result<Redirect, Template> {
    let user = get_user(&login_form.username).unwrap();
    match user {
        Some(existing_user) => if verify(&login_form.password, &existing_user.password_hash).unwrap() {
            return Ok(Redirect::to(uri!(home)))
        },
        // we now hash (without verifying) just to ensure that the timing is the same
        None => {
            hash(&login_form.password, DEFAULT_COST);
        },
    };
    let mut response = Template::render("login", &LoginContext {
        error: Some(String::from("Invalid username or password!")),
    });
    // TODO: <<<<<<<<<< HOW CAN I SET AN HTTP STATUS CODE TO THE RESPONSE?
    Err(response)
}

I am trying to set the HTTP status response code, but I can't find the correct method to do it? It would be good to notify the browser that the login was not successful with something other than a 200.


回答1:


From the Template docs (specifically the Responder trait):

Returns a response with the Content-Type derived from the template's extension and a fixed-size body containing the rendered template. If rendering fails, an Err of Status::InternalServerError is returned.

Try making a new Error enum that derives the Responder trait. Instead of returning Result<Redirect, Template>, return Result<Redirect, Error> where Error looks like this:

#[derive(Debug, Responder)]
enum Error {
    #[response(status = 400)]
    BadRequest(Template),
    #[response(status = 404)]
    NotFound(Template),
}


来源:https://stackoverflow.com/questions/60908423/how-can-i-set-the-http-status-code-of-a-rust-rocket-api-endpoints-template-re

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!