liveBook · Manning

HTTP client

As explained in Rust Servers, Services, and Apps

An HTTP client is software that constructs and sends HTTP requests and processes the resulting HTTP responses.

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

How Rust Servers, Services, and Apps covers it

How deep it goes
Builds the request, awaits the body, converts bytes, and deserializes JSON, while leaving broader client concerns unaddressed.
Examples
Uses a tutor-list endpoint, a strongly typed Tutor structure, iter4.rs, and a Tera-rendered /tutors page.
What it assumes
Assumes an Actix Web application, a reachable REST service, JSON responses, and Tera template rendering.
What you can do after
Apply it via Actix Web and serde_json to retrieve typed JSON from a backend service.
How it connects
Connects HTTP clients to REST APIs, JSON, serde_json, Actix Web, and database access logic.

Also explained in

Compare how 4 books explain HTTP client →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust Servers, Services, and Apps. The section links point to the parts of the book it draws on.