官术网_书友最值得收藏!

URI query design

It is perfectly acceptable to use a query string as part of an API call; however, I would refrain from using this to pass data to the service. Instead the query should be used to perform actions such as:

  • Paging
  • Filtering
  • Sorting

If we need to make a call to a controller, we discussed earlier that we should use a POST request as this is most likely a non-idempotent request. To pass data to the service, we should include the data inside of the body. However, we could use a query string to filter the action of the controller:

POST /sendStatusUpdateEmail?$group=admin 
{
"message": "All services are now operational\nPlease accept our
apologies for any inconvenience caused.\n
The Kitten API team"
}

In the preceding example, we would send a status update email with the message included in the body of the request, because we are using the group filter passed in the query string we could restrict the action of this controller to only send to the admin group.

If we had added the message to the query string and not passed a message body, then we would potentially be causing two problems for ourselves. The first is that the max length for a URI is 2083 characters. The second is that generally a POST request would always include a request body. Whilst this is not required by the HTTP specification, it would be expected behavior by the majority of your users.

主站蜘蛛池模板: 太白县| 玉门市| 万安县| 盐津县| 南安市| 班玛县| 虎林市| 武城县| 庆城县| 屏东县| 井陉县| 南通市| 新密市| 安陆市| 政和县| 大丰市| 台安县| 塔河县| 都安| 寻乌县| 忻城县| 隆安县| 铜陵市| 西盟| 盐源县| 垦利县| 平湖市| 景洪市| 讷河市| 津南区| 佳木斯市| 金塔县| 双鸭山市| 高陵县| 海原县| 周口市| 德钦县| 阜城县| 襄樊市| 白银市| 怀远县|