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

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.

主站蜘蛛池模板: 泸溪县| 汝州市| 青浦区| 清镇市| 正定县| 托里县| 通河县| 揭西县| 沙湾县| 石嘴山市| 长海县| 海阳市| 新河县| 乌拉特后旗| 化隆| 浮山县| 侯马市| 白银市| 乌恰县| 定西市| 会宁县| 阳西县| 曲水县| 许昌市| 通辽市| 体育| 缙云县| 田林县| 荥阳市| 南京市| 景泰县| 丰城市| 阿拉尔市| 新源县| 达州市| 普宁市| 上栗县| 永嘉县| 云和县| 赫章县| 靖江市|