- Building Microservices with Go
- Nic Jackson
- 244字
- 2021-07-15 17:28:09
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.
- Spring Boot 2實(shí)戰(zhàn)之旅
- Learning C# by Developing Games with Unity 2020
- Java應(yīng)用與實(shí)戰(zhàn)
- UML和模式應(yīng)用(原書第3版)
- Visual Basic 6.0程序設(shè)計(jì)計(jì)算機(jī)組裝與維修
- Python進(jìn)階編程:編寫更高效、優(yōu)雅的Python代碼
- Python Network Programming Cookbook(Second Edition)
- Mastering LibGDX Game Development
- 概率成形編碼調(diào)制技術(shù)理論及應(yīng)用
- HTML5+CSS3網(wǎng)頁(yè)設(shè)計(jì)
- Learning Probabilistic Graphical Models in R
- HTML5秘籍(第2版)
- SQL Server實(shí)用教程(SQL Server 2008版)
- 運(yùn)維前線:一線運(yùn)維專家的運(yùn)維方法、技巧與實(shí)踐
- Java Web從入門到精通(第2版)