如何发现非 SOAP Web API 的契约?
假设我有 RESTful 或其他无合约 API 的 URI。是否可以通过编程方式确定其接口?我正在使用 C#/ASP.net MVC,不确定这是否重要。
我知道不会有公开的合同,但我想知道是否有什么我没有想到的(通常是)。
Let's say I have the URI for a RESTful or other contract-less API. Is it possible to determine its interface programatically? I'm using C#/ASP.net MVC, not sure if that's important.
I understand that there won't be a published contract, but I'm wondering if there's something I'm not thinking of (there usually is).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,不幸的是,你是对的。除了已发布的文档之外,通常没有与 RESTful API 相关的合约。
No. Unfortunately, you're right. There are usually no contracts with RESTful APIs other than the published documentation of it.
如果系统是 RESTful 的,那么返回的表示将为您提供可用端点的链接。这些链接很容易通过编程方式发现。请参阅此示例。
内容类型 HTTP 标头应该告诉您返回的信息类型。假设您的客户端应用程序了解该媒体类型,那么您应该拥有使用该服务所需的所有信息。
不幸的是,返回通用媒体类型(例如 application/xml 和 application/json)已经变得司空见惯,它们除了告诉您如何解析内容之外,什么也不告诉您。如果是这种情况,那么您的客户端将需要存储一些关于预期来自特定 URL 的数据类型的附加信息。这是令人讨厌的耦合,但在人们开始真正理解“自描述”REST 约束之前,这是一种不可避免的罪恶。
If the system is RESTful then the returned representation will provide you with links to the available endpoints. These links are easy to discover programatically. See this example.
The content-type HTTP header should tell you what type of information is being returned. Assuming your client application understands that media type then you should have all the information you need to know to use the service.
Unfortunately, it has become commonplace to return generic media types like application/xml and application/json that tells you nothing more about the content than how to parse it. If that is the case then your client will need to store some additional information about what datatype is expected to come from a particular URL. This is nasty coupling but is a necessary evil until people start really to understand the "self-descriptive" REST constraint.