For requests with HTTP methods of POST, PUT, DELETE, usually we will need to define the request body. With RAML, how we will define the structure of the request body and how to define the response with RAML. In this tutorial, let’s find out together!
Suppose I define a request to add new student information with the following initial content:
1 2 3 4 5 6 7 8 |
#%RAML 1.0 baseUri: https://localhost:8081/api title: Student Management System version: 1.0 /students: post: description: Add new student |
To define the request body for this request, we will declare an additional section body with content-type, data type, and an example for this request body. Examples are as follows:
1 2 3 4 5 6 7 |
/students: post: description: Add new student body: application/json: type: Student example: { "id" : 5, "name" : "Khanh" } |
As you can see, I have defined the content-type for the data in the request body in my example as application/json with the data type of Student as follows:
1 2 3 4 5 6 7 8 9 10 |
types: Student: type: object properties: id: required: true type: integer name: required: true type: string |
To define the response for a request in RAML, we will use the responses section.
RAML allows us to define response for each HTTP status code and for each response, similar to the request body, we can also define response body. Examples are as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/students: post: description: Add new student body: application/json: type: Student example: { "id" : 5, "name" : "Khanh" } responses: 201: body: application/json: type: String example: "Created" |
As you can see, right after declaring the section for the HTTP status code, we will declare the section body to declare the content of the response body. We can also declare content-type, data type, example similar to the request body.