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".to_string()};
  • Write html text


Write status error

  • Use render can write a http error to response.

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

    use salvo::http::StatusCode;

Redirect to Another URL

  • Use the render method to write a redirect response into Response, which navigates to a new URL. When you invoke the Redirect::found method, it sets the HTTP status code to 302 (Found), indicating a temporary redirect.
    use salvo::prelude::*;
    async fn redirect(res: &mut Response) {