January 2019
Intermediate to advanced
520 pages
14h 32m
English
When data is created, we need to be able to read it. For this case, we can use the HTTP GET method. Add the following branch to the code:
(&Method::GET, Some(id)) => { if let Some(data) = users.get(id) { Response::new(data.to_string().into()) } else { response_with_code(StatusCode::NOT_FOUND) } }
This code uses the user database to try to find the user by the ID that's provided in the path. If the user is found, we'll convert its data to a String and into a Body to send with a Response.
If the user isn't found, the handler branch will respond with the NOT_FOUND status code (the classic 404 error).
To make the UserData convertible to a String, we have to implement the ToString trait for that type. However, it's typically ...
Read now
Unlock full access