RESTful编程到底是什么?RESTful编程到底是什么?
4 回答
Qyouu
TA贡献1786条经验 获得超11个赞
对基本资源的请求/
可能会返回如下内容:
请求
GET / Accept: application/json+userdb
反应
200 OK Content-Type: application/json+userdb { "version": "1.0", "links": [ { "href": "/user", "rel": "list", "method": "GET" }, { "href": "/user", "rel": "create", "method": "POST" } ] }
我们从媒体的描述中得知,我们可以从称为“链接”的部分找到有关相关资源的信息。这叫做超媒体控件。在这种情况下,我们可以从这样的部分判断,我们可以通过发出另一个请求来找到一个用户列表。/user
:
请求
GET /user Accept: application/json+userdb
反应
200 OK Content-Type: application/json+userdb { "users": [ { "id": 1, "name": "Emil", "country: "Sweden", "links": [ { "href": "/user/1", "rel": "self", "method": "GET" }, { "href": "/user/1", "rel": "edit", "method": "PUT" }, { "href": "/user/1", "rel": "delete", "method": "DELETE" } ] }, { "id": 2, "name": "Adam", "country: "Scotland", "links": [ { "href": "/user/2", "rel": "self", "method": "GET" }, { "href": "/user/2", "rel": "edit", "method": "PUT" }, { "href": "/user/2", "rel": "delete", "method": "DELETE" } ] } ], "links": [ { "href": "/user", "rel": "create", "method": "POST" } ] }
从这个反应中我们可以看出很多。例如,我们现在知道我们可以通过以下方式创建一个新用户POST
使./user
:
请求
POST /user Accept: application/json+userdb Content-Type: application/json+userdb { "name": "Karl", "country": "Austria" }
反应
201 Created Content-Type: application/json+userdb { "user": { "id": 3, "name": "Karl", "country": "Austria", "links": [ { "href": "/user/3", "rel": "self", "method": "GET" }, { "href": "/user/3", "rel": "edit", "method": "PUT" }, { "href": "/user/3", "rel": "delete", "method": "DELETE" } ] }, "links": { "href": "/user", "rel": "list", "method": "GET" } }
我们还知道,我们可以改变现有的数据:
请求
PUT /user/1 Accept: application/json+userdb Content-Type: application/json+userdb { "name": "Emil", "country": "Bhutan" }
反应
200 OK Content-Type: application/json+userdb { "user": { "id": 1, "name": "Emil", "country": "Bhutan", "links": [ { "href": "/user/1", "rel": "self", "method": "GET" }, { "href": "/user/1", "rel": "edit", "method": "PUT" }, { "href": "/user/1", "rel": "delete", "method": "DELETE" } ] }, "links": { "href": "/user", "rel": "list", "method": "GET" } }
注意,我们使用的是不同的HTTP谓词(GET
, PUT
, POST
, DELETE
(等等)为了操纵这些资源,我们认为客户所掌握的唯一知识就是我们的媒体定义。
白衣非少年
TA贡献1155条经验 获得超0个赞
POST /user fname=John&lname=Doe&age=25
200 OK Location: /user/123
GET /user/123
200 OK <fname>John</fname><lname>Doe</lname><age>25</age>
lname
age
PATCH /user/123 fname=Johnny
lname
age
PUT /user/123 fname=Johnny
添加回答
举报
0/150
提交
取消