CPS251 Android Development by Scott Shaper

REST API

Imagine you have a restaurant, and your customers (which are apps) want to order food (data). Instead of shouting their orders directly to the kitchen (database), they speak to a waiter (API). The waiter then communicates with the kitchen, gets the food, and brings it back to the customer.

A REST API (Representational State Transfer Application Programming Interface) is like a standardized, well-organized waiter for web services. It's a set of rules and conventions that allows different software applications to communicate with each other over the internet.

In simpler terms, it's how your mobile app talks to a server to get information (like weather data, news, or social media posts) or send information (like posting a comment or updating a profile).

Core REST Principles (Constraints)

RESTful APIs are designed around a few key principles that make them efficient, scalable, and easy to use:

HTTP Methods (Verbs) and Resources

In a RESTful API, you interact with resources (which can be any data or service, like a user, a product, or weather information) using standard HTTP methods (also known as verbs).

Each method has a specific purpose for performing actions on a resource:

HTTP Method Description Typical Usage Idempotent?
GET Retrieves data from a specified resource. Fetching a list of users, getting weather data. Yes
POST Submits data to a specified resource, often creating a new resource. Creating a new user, submitting a form. No
PUT Updates an existing resource, or creates it if it doesn't exist, by completely replacing it. Updating all fields of a user's profile. Yes
PATCH Applies partial modifications to a resource. Updating only a few fields of a user's profile (e.g., changing email only). No
DELETE Deletes a specified resource. Removing a user account, deleting a post. Yes

Idempotent means that making the same request multiple times will have the same effect as making it once. For example, deleting a resource multiple times will still result in the resource being deleted, and getting a resource multiple times will return the same data.

Learning Aids

Tips for Success

Common Mistakes to Avoid

Best Practices