An HTTP client lets a server-side web application call a backend service instead of duplicating database access or exposing database credentials. section 7.7
A practical Actix Web integration:
- Create an Actix Web Client and send a GET request to the tutor REST endpoint. section 7.7
- Await the response body, interpret its bytes as UTF-8, and deserialize the JSON into Vec<Tutor> with serde_json. section 7.7
- Pass the retrieved tutors to a Tera template for rendering. section 7.7
Tutor list handler
In iter4.rs, handle_get_tutors requests http://localhost:3000/tutors/, converts the response into Tutor values, and serves them at /tutors. section 7.7
use actix_web::client::Client;
async fn handle_get_tutors(tmpl: web::Data<tera::Tera>) ->
Result<HttpResponse, Error> {
let client = Client::default(); #1
// Create request builder and send request
let response = client
.get("http://localhost:3000/tutors/") #2
.send() #3
.await #4
.unwrap() #5
.body() #6
.await #4
.unwrap(); #5
let str_list = std::str::from_utf8(&response.as_ref()).unwrap(); #7
let tutor_list: Vec<Tutor> = serde_json::from_str(str_list).unwrap(); #8
let mut ctx = tera::Context::new();
ctx.insert("tutors", &tutor_list); #9
let rendered_html = tmpl
.render("list.html", &ctx)
.map_err(|_| error::ErrorInternalServerError("Template error"))?;
Ok(HttpResponse::Ok().content_type("text/html").body(rendered_html))
}
Shows the Client construction, request sending, asynchronous response-body retrieval, and byte conversion. section 7.7



