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

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.

主站蜘蛛池模板: 宾阳县| 诸城市| 清丰县| 榆社县| 伊金霍洛旗| 湘潭市| 辰溪县| 舞阳县| 吉林省| 靖江市| 那坡县| 常山县| 阳曲县| 安顺市| 牡丹江市| 康乐县| 柳江县| 榆社县| 东至县| 云南省| 界首市| 绥滨县| 吴川市| 图片| 错那县| 综艺| 常熟市| 鄱阳县| 贺州市| 桃园县| 海城市| 巴塘县| 凉城县| 双鸭山市| 民县| 南昌市| 泗水县| 桃江县| 河北区| 铜川市| 全州县|