- 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.
- Learning NServiceBus(Second Edition)
- 小創(chuàng)客玩轉(zhuǎn)圖形化編程
- SpringMVC+MyBatis快速開發(fā)與項(xiàng)目實(shí)戰(zhàn)
- Android 7編程入門經(jīng)典:使用Android Studio 2(第4版)
- Learning Linux Binary Analysis
- 從0到1:Python數(shù)據(jù)分析
- 數(shù)據(jù)結(jié)構(gòu)習(xí)題解析與實(shí)驗(yàn)指導(dǎo)
- HTML5 APP開發(fā)從入門到精通(微課精編版)
- Spring 5 Design Patterns
- Troubleshooting Citrix XenApp?
- NGUI for Unity
- MATLAB計(jì)算機(jī)視覺實(shí)戰(zhàn)
- Python自動化運(yùn)維:技術(shù)與最佳實(shí)踐
- iOS應(yīng)用逆向工程:分析與實(shí)戰(zhàn)
- Go語言Hyperledger區(qū)塊鏈開發(fā)實(shí)戰(zhàn)