9 ms·
use actix_web::{web, App, HttpServer, Responder}; fn index(info: web::Path<(u32, String)>) -> impl Responder { format!("Hello {}! id:{}", info.
by MuffinFlavored 7y ago
use actix_web::{web, App, HttpServer, Responder};
fn index(info: web::Path<(u32, String)>) -> impl Responder {
format!("Hello {}! id:{}", info.1, info.0)
}
fn main() -> std::io::Result<()> {
HttpServer::new(
|| App::new().service(
web::resource("/{id}/{name}/index.html").to(index)))
.bind("127.0.0.1:8080")?
.run()
}
How do you know the HTTP method your path is called with?
- couchand 7y agoI haven't dug into the 1.0 API, but in the 0.7 line you specify an alternative handler signature that includes the HttpRequest object.
- timdorr 7y agoYou would route that resource to the appropriate handler: https://docs.rs/actix-web/1.0.0/actix_web/struct.Resource.html#method.route https://docs.rs/actix-web/1.0.0/actix_web/struct.Resource.ht...
- iBelieve 7y agoWhat couchand said still applies to 1.0. So this should work: use actix_web::{web, App, HttpServer, HttpRequest, Responder}; fn index(req: HttpRequest, info: web::Path<(u32, String)>) -> impl Responder { format!("[{}] Hello {}! id:{}", req.method(), info.1, info.0) }
- deleted 7y ago[deleted]
- Dowwie 7y agothat example isn't very good.. here's what the resource-registration for "/products" endpoint would look like (note the http method), where I've lazily omitted "/products" a level above because I wanted you just to see the http methods: web::resource("") .route(web::get().to_async(products::get_products)) .route(web::post().to_async(products::add_product))
- mdtusz 7y agoThis syntax makes it look almost like [warp's](https://github.com/seanmonstar/warp https://github.com/seanmonstar/warp) "filters".
- iruoy 7y agoYou can add multiple routes for a resource. App.new().service( web::resource("/welcome") .route(web::get().to(welcome)) .route(web::post().to(post_handler))