liveBook · Manning

HTTP GET

As explained in Learn Rust in a Month of Lunches

HTTP GET is a request method for retrieving a resource or representation from a server.

HTTP GET is a request for something at a URL. Viewing a webpage makes a GET request; the server can respond with HTML, JSON, or other content. A browser or phone may make many such requests in a day. section 19.1 section 24.1.1

In Rust, a reqwest `Client` can start a request with `.get("https://www.rust-lang.org")`. This returns a `RequestBuilder`, which can be configured before `.send()` sends the request. The ordinary client is asynchronous: calling `.send().unwrap()` immediately produces a compiler error because `.send()` returns a `Future`, not a completed response. For a few requests, `reqwest::blocking::Client` is presented as another option. section 19.1

A server needs somewhere to receive the request. With Axum, `.route("/", get(handler))` connects GET requests for `/` to a handler function. A route such as `/double/:number` can take a value from the URL and return a calculated string. If a requested path has no route, the browser can show a 404 error. section 24.1.1

use axum::{extract::Path, routing::get};
 
async fn double(Path(input): Path<String>) -> String {                     #1
    match input.parse::<i32>() {                                           #2
        Ok(num) => format!("{} times 2 is {}!", num, num * 2),
        Err(e) => format!("Uh oh, weird input: {e}")
    }
}
 
#[tokio::main]
async fn main() {
    let app = axum::Router::new()
        .route("/", get(|| async { "The server works!" }))                 #3
        .route(                                                            #4
            "/game/:guess",
            get(|Path(guess): Path<String>| async move { format!("The guess
            is {guess}") }),
        )
        .route("/double/:number", get(double));                            #5
 
    axum::Server::bind(&"127.0.0.1:8080".parse().unwrap())
        .serve(app.into_make_service())
        .await
        .unwrap();
 
}
#1 The signature here may look a bit odd, but it is just deconstructing the input inside the function signature. Axum uses this syntax a lot, so we’ll copy it.
#2 Nothing else in this function is particularly surprising: it tries to parse a string into an <kbd>i32</kbd> and doubles it if it can.
#3 This first route just lets us know that the server works, and we handle the response inside an async closure.
#4 For the second route, we will also use an async function. The colon in <kbd>:guess</kbd> means to pass in whatever is after <kbd>/game/</kbd> to the function under the variable name <kbd>guess</kbd>. This time, we need <kbd>async move</kbd> because we want the function to take ownership of <kbd>guess</kbd>. Then we will return the guess to the user.
#5 For the third route, we will use the previous doubling function. Here, as well, we pass in the path as the variable <kbd>number</kbd>, but inside the function itself, it will have a different name.

The Axum example connects three paths to GET handlers: one returns a greeting, one reads a guess from the path, and one doubles a number. section 24.1.1

How Learn Rust in a Month of Lunches covers it

How deep it goes
Explains reqwest request building and sending, then sketches Axum routing without fully explaining Axum or async internals.
Examples
Shows a request to rust-lang.org and localhost routes that greet visitors, echo a guess, or double an integer.
What it assumes
Defines GET through ordinary webpage visits and defers explanation of Future and async.
What you can do after
Build it from scratch with Axum GET routes and apply it via reqwest’s Client to request a URL.
How it connects
Connects GET to URLs, HTTP responses, request handlers, async Rust, and Axum routing.

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 Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.