liveBook · Manning

HTTP GET

As explained in Rust Servers, Services, and Apps

GET is an HTTP request method for retrieving a representation of a resource.

HTTP GET retrieves a resource representation. In a tutor application, GET handlers fetch all tutors or one tutor by path parameter, delegate to database functions, and return JSON in an HTTP response. section 6.3.2

A web application can issue GET to a backend REST API, await the response, decode its JSON payload into a strongly typed Rust collection, and render the results with a template. section 7.7

Retrieving tutors with Actix Web

A handler creates an HTTP client, calls GET on http://localhost:3000/tutors/, awaits the response body, deserializes it into Vec<Tutor>, and supplies the tutors to the page template. 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))
}

Inspect the incremental Actix Web client implementation, including request construction, asynchronous sending, body retrieval, and response decoding. section 7.7

At the server boundary, a minimal Rust HTTP implementation recognizes GET during request-line parsing and converts it into a Method::Get enum variant. section 2.3.1

How Rust Servers, Services, and Apps covers it

How deep it goes
Implements GET from request-line parsing through asynchronous client calls and handlers, while deferring broader HTTP method support beyond GET and POST.
Examples
Uses tutor-list and tutor-detail handlers, a localhost tutor endpoint, request-line parser tests, and rendered tutor-list templates.
What you can do after
Build it from scratch as a Rust request parser or apply it through Actix Web to retrieve and render typed JSON data.
How it connects
Connects HTTP requests with REST APIs, HTTP responses, JSON, database access, Rust, and Actix Web.

Also explained in

Compare how 14 books explain HTTP GET →

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.