Overview

3 Create your first route handler

This chapter introduces the basics of building a Rust web service with Warp by first setting up the project structure, required dependencies, and the runtime stack that powers the server. It explains why Warp is a practical choice for the book’s Q&A application, emphasizing its active community, strong documentation, and the fact that it builds on Tokio for async execution and Hyper for HTTP handling.

The core lesson is how Warp processes requests through chained filters, which together form routes. A simple first handler is created for a GET request that matches a specific path, calls a route-handling function, and returns JSON. To make that work, the book shows how to structure the handler’s return type, how to use Serde’s serialization support to turn custom Rust data into JSON, and how Warp’s rejection system lets you handle failures cleanly instead of crashing or returning vague responses.

The chapter closes by showing how to customize error handling and prepare the service for browser use with CORS. It demonstrates creating custom rejection types, mapping them to meaningful HTTP status codes, and using Warp’s built-in CORS filter to allow selected origins, headers, and methods. By the end, the reader has a working route, JSON output, graceful error responses, and a server configured to interact safely with browsers across origins.

The web framework Warp includes Tokio as its runtime and Hyper as a HTTP server library.
03-01
Workflow of sending, parsing, and answering HTTP GET requests with Warp
03-02
As an HTTP request passes through each filter, facing possible rejections along the way, those rejections can be picked up by Warp’s recover method to send out custom HTTP responses.
03-03
Getting an HTTP request, sending it through Warp filters and the route handler, and then returning a proper HTTP message
03-04
In a CORS workflow, a user initiates a POST request inside a browser, and the browser first does a preflight HTTP OPTIONS request to the server, which has to respond with allowed domains (origins), methods, and headers.
03-05

Summary

  • It is important to understand which stack is covered by the library you are choosing.
  • Usually, you have to include a runtime to support your chosen web framework’s asynchronous way of working.
  • Every web framework already comes with a web server and types to return proper HTTP messages.
  • Try to understand the mindset behind the framework you are choosing and think through a few use cases and how they would be implemented with this mindset.
  • Start small and with the happy path, which is the GET route for one particular resource, in most cases.
  • Use the Serde library to serialize and deserialize structs you create.
  • Immediately start thinking about unsuccessful paths and implement custom error handling afterward.
  • When HTTP requests come from a browser and originate from a different domain than the one our server is deployed under, we have to handle OPTION requests, which are part of the CORS workflow.
  • The Warp framework has a built-in cors filter that can answer the requests appropriately.

FAQ

What is the main goal of Chapter 3 in Rust Web Development?The chapter introduces the basics of building your first route handler with Warp, returning JSON from a web server, handling errors with proper HTTP status codes, and setting up CORS support.
Why does the book choose Warp as the web framework for this chapter?Warp is chosen because it is lightweight, actively maintained, well documented, built on Tokio, and has an active community that can help when you run into problems.
What two core dependencies does a Warp project need?A Warp project needs Warp itself and Tokio. Warp uses Hyper under the hood for HTTP handling, but Tokio must be added separately to your own project.
What is Warp’s filter system used for?Warp’s filter system is how requests are matched, processed, and transformed. Filters are chained together to check paths, HTTP methods, headers, JSON bodies, and more before a request reaches a route handler.
How does Warp decide which route handler to call?Warp checks the incoming request path and HTTP method against the filters you defined. If the request matches, Warp forwards it to the route handler attached with .and_then.
How do you create a simple GET route in Warp?You can combine filters like warp::get(), warp::path("questions"), and warp::path::end(), then attach a handler function with .and_then(...).
Why does a route handler return Result<impl warp::Reply, warp::Rejection>?This return type lets the handler either send a successful response that implements warp::Reply or return a warp::Rejection for error handling.
How do you return JSON from a route handler in Warp?You return JSON with warp::reply::json(&value), but the value must implement Serde’s Serialize trait.
Why do structs like Question need the Serialize trait?Warp’s JSON reply helper uses Serde serialization. Any custom struct you want to send as JSON must implement Serialize, including nested custom types.
How does the chapter handle errors and CORS failures?The chapter shows how to define a custom rejection type, recover from rejections with .recover(...), map errors to HTTP status codes like 404, 422, and 403, and enable CORS with Warp’s cors() filter.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Rust Web Development Rust Web Development ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Rust Web Development Rust Web Development ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Rust Web Development Rust Web Development ebook for free