POST /api/level0/operation HTTP/1.1
Content-Type: application/json;charset=UTF-8
Accept: application/json
Host: localhost:8080
Content-Length: 86
{
"command" : "createProduct",
"parameters" : {
"title" : "Test product 123"
}
}
Representational State Transfer (REST) is a software architectural style that defines a set of constraints to be used for creating Web services.
The starting point for the model is using HTTP as a transport system for remote interactions, but without using any of the mechanisms of the web.
Use one or a few URL endpoint to receive all request
Send all request by one HTTP method, mostly POST
Always response HTTP status 200 regardless of sucess or fail
Contain verbs in request body
Contain status in response body
POST /api/level0/operation HTTP/1.1
Content-Type: application/json;charset=UTF-8
Accept: application/json
Host: localhost:8080
Content-Length: 86
{
"command" : "createProduct",
"parameters" : {
"title" : "Test product 123"
}
}
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 164
{
"status" : "SUCCESS",
"message" : "",
"parameters" : {
"id" : 3,
"title" : "Test product 123",
"createdAt" : "Sun Mar 11 15:43:11 CST 2018"
}
}
The first step towards the Glory of Rest in the RMM is to introduce resources. So now rather than making all our requests to a singular service endpoint, we now start talking to individual resources.
Expose multiple URL endpoints
Send all request by one HTTP method, mostly POST
Always response HTTP status 200 regardless of sucess or fail
Contain verbs in request body
Contain status in response body
Define resources which used in many reqeuest/response payload
POST /api/level1/product HTTP/1.1
Content-Type: application/json
Accept: application/json
Host: localhost:8080
Content-Length: 117
{
"command" : "create",
"data" : {
"id" : null,
"title" : "Test product 123",
"createdAt" : null
}
}
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Content-Length: 160
{
"status" : "SUCCESS",
"message" : null,
"data" : {
"id" : 5,
"title" : "Test product 123",
"createdAt" : "2018-03-11T07:43:11.840+0000"
}
}
Level 2 moves away from this, using the HTTP verbs as closely as possible to how they are used in HTTP itself.
Expose multiple URL endpoints
Each URL endpoint only accept fixed structure request/reposne payload
Define resources which used in many reqeuest/response paylaod
Use HTTP Verbs to represent action
Use HTTP Status to represent result
POST /api/level2/product HTTP/1.1
Content-Type: application/json
Accept: application/json
Host: localhost:8080
Content-Length: 73
{
"id" : null,
"title" : "New product title",
"createdAt" : null
}
HTTP/1.1 201 Created
Content-Type: application/json;charset=UTF-8
Content-Length: 97
{
"id" : 21,
"title" : "New product title",
"createdAt" : "2018-04-30T12:02:08.667+0000"
}
The point of hypermedia controls is that they tell us what we can do next, and the URI of the resource we need to manipulate to do it. Rather than us having to know where to post our appointment request, the hypermedia controls in the response tell us how to do it.
Expose multiple URL endpoints
Each URL endpoint only accept fixed structure request/reposne payload
Define resources which used in many reqeuest/response paylaod
Use HTTP Verbs to represent action
Use HTTP Status to represent result
Tell us links to other resources in response
POST /products HTTP/1.1
Accept: application/json
Content-Type: application/json;charset=UTF-8
Content-Length: 365
Host: localhost:8080
{
"title" : "New Product",
"tags" : [ "Electronics", "Mobile" ],
"images" : [ "http://localhost:8080/productImages/5d958ccadf86bd1cd947dbc6", "http://localhost:8080/productImages/5d958ccadf86bd1cd947dbc7" ],
"variants" : [ "http://localhost:8080/productVariants/5d958ccadf86bd1cd947dbc9", "http://localhost:8080/productVariants/5d958ccadf86bd1cd947dbca" ]
}
HTTP/1.1 201 Created
Content-Length: 589
Content-Type: application/json;charset=UTF-8
Location: http://localhost:8080/products/5d958ccadf86bd1cd947dbcc
{
"title" : "New Product",
"tags" : [ "Electronics", "Mobile" ],
"createdAt" : "2019-10-03T05:53:14.592+0000",
"updatedAt" : "2019-10-03T05:53:14.592+0000",
"_links" : {
"self" : {
"href" : "http://localhost:8080/products/5d958ccadf86bd1cd947dbcc"
},
"product" : {
"href" : "http://localhost:8080/products/5d958ccadf86bd1cd947dbcc"
},
"images" : {
"href" : "http://localhost:8080/products/5d958ccadf86bd1cd947dbcc/images"
},
"variants" : {
"href" : "http://localhost:8080/products/5d958ccadf86bd1cd947dbcc/variants"
}
}
}
PUT /products/5d958ccadf86bd1cd947dbc5/images HTTP/1.1
Content-Type: text/uri-list;charset=UTF-8
Content-Length: 121
Host: localhost:8080
http://localhost:8080/productImages/5d958ccadf86bd1cd947dbc0
http://localhost:8080/productImages/5d958ccadf86bd1cd947dbc1