HTTP API的最后一个常用用法是获取服务器上现有资源的列表。此类列表应使用GET请求获得,因为它们仅检索数据。
如果服务器200 OK可以提供列表,则服务器应返回;否则,服务器应返回适当的错误代码。
这样,列出我们的员工可能看起来像这样:
GET /employees HTTP/1.1 Host: example.com
HTTP/1.1 200 OK Content-Type: application/json { 'employees': [ { 'name': 'Charlie Smith', 'age': 39, 'job_title': 'Software Developer', 'salary': 63985.00 'links': [ { 'uri': '/employees/1/charlie-smith', 'rel': 'self', 'method': 'GET' }, { 'uri': '/employees/1/charlie-smith', 'rel': 'delete', 'method': 'DELETE' }, { 'uri': '/employees/1/charlie-smith', 'rel': 'edit', 'method': 'PATCH' } ] }, { 'name': 'Donna Prima', 'age': 30, 'job_title': 'QA Tester', 'salary': 77095.00 'links': [ { 'uri': '/employees/2/donna-prima', 'rel': 'self', 'method': 'GET' }, { 'uri': '/employees/2/donna-prima', 'rel': 'delete', 'method': 'DELETE' }, { 'uri': '/employees/2/donna-prima', 'rel': 'edit', 'method': 'PATCH' } ] } ], 'links': [ { 'uri': '/employees/new', 'rel': 'create', 'method': 'PUT' } ] }