A path extractor reads typed values from URL path parameters and supplies them to an Actix Web handler. Routes declare placeholders such as {tutor_id} and {course_id}; handlers receive those values through web::Path, while request bodies use separate JSON extractors. section 6.2.2
Match the extractor type to the route parameters:
- Use web::Path<i32> for a route containing one identifier, then call into_inner() to obtain the integer. section 6.2.2
- Use web::Path<(i32, i32)> for nested identifiers such as tutor_id and course_id, then destructure the tuple. section 6.2.2
- Pass extracted identifiers to database access functions, which can return successful responses or not-found errors. section 6.2.2
Extracting two resource identifiers
pub async fn get_course_details( app_state: web::Data<AppState>, path: web::Path<(i32, i32)>, ) -> Result<HttpResponse, EzyTutorError> { let (tutor_id, course_id) = path.into_inner(); get_course_details_db(&app_state.db, tutor_id, course_id).await .map(|course| HttpResponse::Ok().json(course)) } section 6.2.2
pub async fn get_course_details(
app_state: web::Data<AppState>,
path: web::Path<(i32, i32)>,
) -> Result<HttpResponse, EzyTutorError> {
let (tutor_id, course_id) = path.into_inner();
get_course_details_db(&app_state.db, tutor_id, course_id)
.await
.map(|course| HttpResponse::Ok().json(course))
}
Inspect the complete handler pattern: receive web::Path, unpack both IDs, call persistence logic, and serialize the result. section 6.2.2
