Resource Representation
You'll learn how a single resource in a REST API can be delivered in different formats to suit different client needs.
The Core Idea
Think of a restaurant menu: the same list of dishes can be printed on paper, displayed on a digital screen, or read aloud by a server. The content (the dishes) stays the same, but the format changes based on who's consuming it.
In REST APIs, resource representation works the same way. A single resource—say, a user profile or product listing—can be represented in multiple formats. The server might send that same data as JSON for a mobile app, XML for a legacy system, or even HTML for a web browser.
The key principle: the resource itself doesn't change, only how it's packaged for delivery.
Why Multiple Representations?
Different clients have different needs:
- JSON is lightweight and perfect for modern web apps and mobile clients
- XML might be required for enterprise systems or older integrations
- HTML lets browsers render the data directly without JavaScript
- CSV could be useful for exporting data to spreadsheets
The same REST endpoint (like /api/users/123) can return any of these formats. The client typically requests its preferred format through HTTP headers (like Accept: application/json) or URL extensions (like /api/users/123.json).
The Stateless Connection
Since REST is stateless, each request must carry all necessary information—including the desired representation format. The server doesn't "remember" what format you wanted last time.
Key Takeaway: Resource representation separates what data is (the resource) from how it's formatted (JSON, XML, HTML), allowing one API to serve diverse clients flexibly.