Sub topic 1
How REST and HTTP work
Prerequisites
This chapter assumes you know Dart classes, null safety, named parameters, and Futures with async and await. You should also know how a Flutter StatefulWidget is built and how to add a package to pubspec.yaml.
A Flutter app usually does not own all the data it displays. It may ask a server for a list of products, send a new comment, or update a user profile. The app and server communicate through HTTP, the protocol used for requests and responses on the web.
REST and resources
REST is a style for designing web APIs. An API is a defined way for one program to request data or actions from another program. A REST API represents things as resources. A resource might be a post, user, invoice, or photo.
Each resource has a URL. For example:
/postsrepresents a collection of posts./posts/42represents the post whose identifier is 42./users/7/ordersrepresents orders belonging to user 7.
REST does not require one exact URL design, but consistent resource URLs make an API easier to understand. The action usually comes from the HTTP method rather than from a verb in the URL.
| HTTP method | Common meaning | Example |
|---|---|---|
| GET | Read a resource | Get post 42 |
| POST | Create a resource | Create a new post |
| PUT | Replace a resource | Replace post 42 |
| PATCH | Partially update a resource | Change only a post title |
| DELETE | Remove a resource | Delete post 42 |
Anatomy of an HTTP exchange
An HTTP request has a method, a URL, headers, and sometimes a body. Headers are named values that describe the request. The Content-Type header tells the server how to interpret the body. An Authorization header can carry a token that proves the caller is signed in.
The server returns an HTTP response. A response contains a status code, headers, and sometimes a body. The status code is a number that describes the result.
- 2xx means success. For example, 200 means OK and 201 means Created.
- 3xx usually means redirection.
- 4xx means the request is invalid or not allowed. For example, 401 means missing or invalid authentication, and 404 means the resource was not found.
- 5xx means the server failed while handling a valid-looking request.
A successful request is not automatically a successful application operation. Your code must check the status code before decoding the response body. A server might return an error page or an error-shaped JSON object instead of the data your model expects.
JSON as the data format
Many REST APIs use JSON, a text format made from objects, arrays, strings, numbers, booleans, and null. A response might look like this:
{
"id": 42,
"title": "Understanding HTTP"
}The JSON text is not automatically a Dart object. The app must decode it and validate or convert its fields. A typed Dart model gives the rest of the application a clear contract. It also keeps JSON-specific parsing near the networking boundary.
For a GET request, the main sequence is:
- Build the URL, including any query parameters.
- Send the request.
- Check whether the status code is acceptable.
- Decode the JSON body.
- Convert the decoded map into a model object.
- Pass the model to the application layer.
The same model applies to other methods. POST, PUT, and PATCH usually send a JSON body. DELETE may return no body at all. The method, status code, headers, and body together describe the exchange, so inspect all four when diagnosing a failed request.
Exercise
A server returns status 201 after a POST request. What does that status usually mean, and what should the client do next?
Show answer
It usually means the resource was created. The client should confirm that 201 is acceptable, then decode the response body if the API returns the created resource.