Module 2
Topic 2

REST APIs

Understanding RESTful APIs — the architecture behind modern web services and how to consume them in Flutter.

Flutter Docs – Networking
What Is REST?

REST (Representational State Transfer) is an architectural style for designing networked applications. REST APIs are the most common way for mobile apps to communicate with servers over the internet.

🌐 The Big Idea

A REST API exposes resources (like users, posts, or products) through URLs (endpoints). Your Flutter app sends HTTP requests to these endpoints to fetch, create, update, or delete data.

REST APIs follow a set of principles that make them scalable , stateless , and cacheable . When you build a Flutter app that talks to a backend, you're almost always working with a REST API.

🔑 REST Principles

  • Stateless – Each request contains all the information needed to process it
  • Cacheable – Responses can be cached to improve performance
  • Client-Server – The client (your app) and server are separate
  • Uniform Interface – Consistent API design using standard HTTP methods
  • Layered System – The API can be composed of multiple layers
Understanding API Endpoints

An endpoint is a specific URL where your app can access a resource. For example, the JSONPlaceholder API provides these endpoints:

GET
/albums/1
Fetch a specific album
POST
/albums
Create a new album
PUT
/albums/1
Update album #1
DELETE
/albums/1
Delete album #1
GET
/posts
Fetch all posts
POST
/posts/1/comments
Add comment to post #1

The full URL combines the base URL with the endpoint path :

Making Different Types of Requests

REST APIs use different HTTP methods for different operations. Here's how to make each type of request in Flutter using the http package.

Step-by-Step Explanation
1.
Define the base URL – Store the API's base URL as a constant. This makes it easy to change if the API moves to a different server.
2.
GET requests – Use client.get() to fetch data. Always set the Accept header to application/json .
3.
POST requests – Use client.post() with a JSON-encoded body. Set both Accept and Content-Type headers.
4.
PUT requests – Use client.put() to update existing resources. Like POST, you need to send JSON data in the body.
5.
DELETE requests – Use client.delete() to remove resources. Success codes are 200 (OK) or 204 (No Content).
Complete Example: Albums App

Here's a complete example that demonstrates fetching, creating, and updating albums using a REST API.

Common REST API Patterns

🔑 Common Patterns

  • Resource Collections /users returns a list of users
  • Individual Resources /users/1 returns user with ID 1
  • Nested Resources /users/1/posts returns posts by user 1
  • Pagination /posts?page=2&limit=10 for paginated results
  • Filtering /posts?userId=1 filters posts by user
  • Search /posts?q=flutter searches for "flutter"
Common Mistakes
❌ Mistake 1: Not handling all HTTP status codes

Only checking for 200 status codes misses other success codes like 201 (Created) and 204 (No Content).

✅ Correct: Handle multiple status codes

Check for 200 , 201 , and 204 as success codes. Throw exceptions for any other status code.

❌ Mistake 2: Forgetting Content-Type header

When sending data with POST or PUT, you must set Content-Type: application/json so the server knows how to parse your request.

✅ Correct: Set both Accept and Content-Type headers

Always set Accept: application/json and Content-Type: application/json when sending or receiving JSON data.

❌ Mistake 3: Hardcoding URLs everywhere

Hardcoding URLs in multiple places makes your code hard to maintain and test.

✅ Correct: Use a base URL constant

Store the base URL in a single place and build full URLs dynamically. This makes it easy to change the API endpoint in one place.

🎯 Key Takeaway

REST APIs are the standard way for Flutter apps to communicate with servers. Learn the HTTP methods (GET, POST, PUT, DELETE), understand endpoints and status codes , and always handle loading and error states . Good API design makes your app scalable and maintainable.