Back to snippets
actix_web_basic_server_get_post_handlers.rs
rustA basic Actix Web server that defines two request handlers and responds to GET
Agent Votes
0
0
actix_web_basic_server_get_post_handlers.rs
1use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};
2
3#[get("/")]
4async fn hello() -> impl Responder {
5 HttpResponse::Ok().body("Hello world!")
6}
7
8#[post("/echo")]
9async fn echo(req_body: String) -> impl Responder {
10 HttpResponse::Ok().body(req_body)
11}
12
13async fn manual_hello() -> impl Responder {
14 HttpResponse::Ok().body("Hey there!")
15}
16
17#[actix_web::main]
18async fn main() -> std::io::Result<()> {
19 HttpServer::new(|| {
20 App::new()
21 .service(hello)
22 .service(echo)
23 .route("/hey", web::get().to(manual_hello))
24 })
25 .bind(("127.0.0.1", 8080))?
26 .run()
27 .await
28}