构建 API URL 的不同方法之间的权衡是什么:子域与子目录和版本控制? [英] What are the trade-offs between different methods of constructing API URLs: subdomain vs. subdirectory and versioning?
问题描述
我们有一个域名为 example.com
的 Web 应用程序.现在我们想将此应用程序的一部分扩展为 REST API,我们正在讨论最佳 URL 模式.
We have a web application with a domain name of example.com
. Now we want to extend a part of this application as a REST API, and we are debating on the best URL pattern.
我们可以使用 URL 模式 api.example.com
或 example.com/api
.如果有的话,需要考虑哪些权衡?
We could use the URL pattern api.example.com
or example.com/api
. What trade-offs are there to consider, if any?
此外,关于 API 版本控制方法有哪些权衡?它可以通过 URL (v1.api.example.com
、example.com/api/v1
或一些奇怪的组合 v1.example.com/api
或 api.example.com/v1
).或者,可以通过使用 HTTP 请求标头(或其他方式)来完成?
Additionally, what trade-offs are there regarding methods of API versioning? It could be done via the URL (v1.api.example.com
, example.com/api/v1
, or some strange mix v1.example.com/api
or api.example.com/v1
). Alternatively, it could be done through the use of HTTP request headers (or otherwise)?
推荐答案
这取决于您的需求.
如果您使用 http://api.example.com,它会使您的 API 成为子域.基本上,如果您的 REST 服务要被多个客户端使用,则此 URL 模式很好,但如果只有一个客户端连接到您的 API,则模式 http://example.com/api/v1 很好.但是,如果您想添加更多 API 并连接更多客户端,最好使用 http://example.com/api/v1.例如,请考虑以下内容.
If you use http://api.example.com it makes your API a subdomain. Basically, this URL pattern is good if your REST service is to be consumed by multiple clients, but if only one client is connected to your API then the pattern http://example.com/api/v1 is good. However, if you want to add more API with more clients connected to it it’s better to use http://example.com/api/v1. For example, consider the following.
http://example.com/reportapi/apioperation?parameters
http://example.com/paymentapi/apioperation?parameters
http://example.com/searchapi/apioperation?parameters
最后但并非最不重要的是,PayPal 使用模式 http://example.com/api/v1.
Last but not least, PayPal uses the pattern http://example.com/api/v1.
这篇关于构建 API URL 的不同方法之间的权衡是什么:子域与子目录和版本控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!