We can get response reference as function handler parameter:

async fn hello_world(res: &mut Response) {
    res.render("hello world!");

When the server gets a client request but you only need to return a simple response (ie skip any middleware or other handlers), you can simply use FlowCtrl:

async fn hello_world(res: &mut Response, ctrl: &mut FlowCtrl) {
    res.render("hello world!");

Write content

Write content is straightforward:

  • Write plain text

    res.render("hello world!");
  • Write serializable type as json format

    #[derive(Serialize, Debug)]
    struct User {
        name: String,
    let user = User{name: "jobs"};
  • Write html text


Write status error

  • Use set_http_error can write a http error to response.

    use salvo::http::errors::*;
    res.set_http_error(StatusError::internal_server_error().with_summary("error when serialize object to json"))
  • If you don't want to customize error message, just use set_http_code.

    use salvo::http::StatusCode;