Skip to main content

Text Responses

Returning plain text

Actix 4.9+ 4 min read

# Text Responses

The simplest form of response in Actix Web is plain text.

#Returning Strings

Actix Web's Responder trait is implemented for String and &'static str. This means you can return strings directly from your handlers, and Actix Web will automatically set the Content-Type to text/plain and the status to 200 OK.

code_blocksrust
use actix_web::{get, Responder};

#[get("/hello")]
async fn hello() -> impl Responder {
    "Hello, world!" // Returns text/plain
}

#Customizing via HttpResponse

If you need more control over status codes or headers, use the HttpResponse builder.

code_blocksrust
use actix_web::{HttpResponse, Responder};

async fn custom_text() -> impl Responder {
    HttpResponse::Accepted()
        .content_type("text/plain")
        .body("Processing request...")
}

Was this page helpful?

Reference Implementation

The following code snippet demonstrates the authoritative pattern for this documentation.

src/main.rs
1use actix_web::{get, App, HttpServer, Responder};
2
3#[get("/")]
4async fn plain_text() -> impl Responder {
5 "Just simple text."
6}
7
8#[actix_web::main]
9async fn main() -> std::io::Result<()> {
10 HttpServer::new(|| App::new().service(plain_text))
11 .bind(("127.0.0.1", 8080))?
12 .run()
13 .await
14}
Was this page helpful?