Resource
The concept of a REST resource is abstract and you can understand it as something that is identified by a URL provided by the server.
The resource can be a user, a list of users, a customer, a file or any entity of the application.
For example, consider a user as your resource with the following attributes and values:
- ID: 1
- First name: John
- Last name: Doe
- e-mail: john.doe@example.com
URL
The URL (Uniform Resource Locator) just identifies the resource, that is, where the resource is located in the server.
For example, while the URL /app/users/1
locates the user with the ID 1
, the URL /app/users
locate all the users in the application.
HTTP methods
REST is protocol independent, but, if you are using HTTP, you can perform actions on the resource accessing the URL with HTTP methods, such as GET
, POST
, PUT
and DELETE
.
For example, when you perform a GET
to the URL /app/users/1
, you'll obtain a representation for the user with the ID 1
.
Resource representation
A resource can be represented in multiple formats, such as JSON, XML, YAML, etc.
In JSON, the representation would be:
{
"id": 1,
"firstName": "John",
"lastName": "Doe",
"email": "john.doe@example.com"
}
In XML, the representation would be the following:
<user>
<id>1</id>
<firstName>John</firstName>
<lastName>Doe</lastName>
<email>john.doe@example.com</email>
</user>
Example 1
Consider you are developing an application in JavaScript and the server can provide a representation of the resources as JSON and XML. It's easier to deal with JSON rather than XML in JavaScript applications. So, you want the resources represented as JSON.
To do it, when performing a GET
to the URL /app/users/1
, you'll add the HTTP header Accept
with the application/json
value to tell the server the representation the client accepts.
Consequently, the server will return the resource represented as JSON. The response will contain the Content-Type
header with the application/json
value, indicating the content of the response is a JSON.
Example 2
Besides JSON and XML, for example, the resources can be represented as images or videos.
Consider a URL which locates the profile picture of a user: /app/users/1/profile-picture
.
Depending the image type, the Content-Type
of the response will be image/jpeg
, image/png
, image/gif
, etc.
This answer may also be insightful.