In the previous tutorial, I introduced you to RAML to define API spec. In this tutorial, I will guide you in more detail how to define request URL with RAML!
As an example for this tutorial, first, I will create a new .raml file with information about the student management API including adding, deleting, editing, searching with the following initial content:
1 2 3 4 |
#%RAML 1.0 baseUri: https://localhost:8081/api title: Student Management System version: 1.0 |
We will define some requests as follows:
- GET /students to get a list of students
- POST /students to add new students
- PUT /students/{id} to update a student with the id passed in the request
- DELETE /students/{id} to delete a student with the id passed in the request
- GET /students/find-by-name?name=<student-name>
- GET /students/find-by-ids?id=<id1>,<id2>
With requests like these, you can define a root request “/students” as follows:
1 |
/students: |
Just imagine a request URL will be divided into several levels from left to right. For example, request /api/students/find will have 3 levels: api, then students and then find. These levels are separated by a “/”. Each level will be a level in the .raml file in that order, so in my example above, the root request for all requests would be “/students”.
For this root request, we have 2 requests, GET and POST, to get all students’ information and add new students, so you just need to declare these requests as follows:
1 2 3 |
/students: get: post: |
You can add a description for each request if you want, for example:
1 2 3 4 5 |
/students: get: description: Get all students post: description: Add new student |
Next, we have 2 requests with /students/{id} with PUT and DELETE HTTP methods, used to update and delete information of a certain student. Since we have already defined the root request with “/students”, now you only need to define these two new requests as follows:
1 2 3 4 5 6 7 8 9 |
/students: get: post: /{id}: put: description: Update information for a student delete: description: Delete information of a student |
You can define more information of the path parameters, information about the purpose of the path parameter, what the data type is, an example of their value. To do this, we will use the uriParameters section. For example, I can define information for path parameter {id} in the above example as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
/students: get: description: Get all students post: description: Add new student /{id}: uriParameters: id: description: Id of the student type: string example: "1" put: description: Update information for a student delete: description: Delete information of a student |
For the request /students/find-by-name?name=<student-name>, we need to define an additional section /find-by-name with the root request as “/students” as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/students: get: description: Get all students post: description: Add new student /{id}: uriParameters: id: description: Id of the student type: string example: "1" put: description: Update information for a student delete: description: Delete information of a student /find-by-name: get: description: Find student by name |
The HTTP method of this request is GET and I have also added a description for this request.
For this /students/find-by-name request, we have an additional request parameter named name. We can define this request parameter information as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
/students: get: description: Get all students post: description: Add new student /{id}: uriParameters: id: description: Id of the student type: string example: "1" put: description: Update information for a student delete: description: Delete information of a student /find-by-name: get: description: Find student by name queryParameters: name: description: Name of student type: string example: "Khanh" |
As you can see, I use the queryParameters section to specify information for all request parameters of the request. The request parameter in this example has data type String. You can define whether this request parameter is required or not using the “required: false” attribute or use the question mark as follows:
1 2 3 4 5 |
queryParameters: name?: description: Name of student type: string example: "Khanh" |
In my example, the request parameter name is required!
For requests with matrix parameters like /students/find-by-ids? id=<id1>,<id2> , you can define that matrix parameter with the type of array. Examples are as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
/students: get: description: Get all students post: description: Add new student /{id}: uriParameters: id: description: Id of the Student type: string example: "1" put: description: Update information for a student delete: description: Delete information of a student /find-by-name: get: description: Find student by name queryParameters: name: description: Name of student type: string example: "Khanh" /find-by-ids: get: description: Find list of students by a list of Ids queryParameters: ids: description: List of Ids type: number[] example: "1,2,3" |
The full contents of the .raml file of the example in this tutorial are as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#%RAML 1.0 baseUri: https://localhost:8081/api title: Student Management System version: 1.0 /students: get: description: Get all students post: description: Add new student /{id}: uriParameters: id: description: Id of the Student type: string example: "1" put: description: Update information for a student delete: description: Delete information of a student /find-by-name: get: description: Find student by name queryParameters: name: description: Name of student type: string example: "Khanh" /find-by-ids: get: description: Find list of students by list of Ids queryParameters: ids: description: List of Ids type: number[] example: "1,2,3" |