简体   繁体   English

如何使用URI作为REST资源?

[英]How to use URI as a REST resource?

I am building a RESTful API for retrieving and storing comments in threads. 我正在构建一个RESTful API,用于在线程中检索和存储注释。

A comment thread is identified by an arbitrary URI -- usually this is the URL of the web page where the comment thread is related to. 注释线程由任意URI标识-通常是与注释线程相关的网页的URL。 This design is very similar to what Disqus uses with their system. 这种设计与Disqus在其系统中使用的设计非常相似。

This way, on every web page, querying the related comment thread doesn't require storing any additional data at the client -- all that is needed is the canonical URL to the page in question. 这样,在每个网页上,查询相关注释线程都不需要在客户端存储任何其他数据-所需的只是有关页面的规范URL。

My current implementation attempts to make an URI work as a resource by encoding the URI as a string as follows: 我当前的实现尝试通过将URI编码为如下字符串来使URI作为资源工作:

/comments/https%3A%2F%2Fexample.org%2Ffoo%2F2345%3Ffoo%3Dbar%26baz%3Dxyz

However, before dispatching it to my application, the request URI always gets decoded by my server to 但是,在将请求发送到我的应用程序之前,服务器总是将请求URI解码为

/comments/https://example.org/foo/2345?foo=bar&baz=xyz

This isn't working because the decoded resource name now has path delimiters and a query string in it causing routing in my API to get confused (my routing configuration assumes the request path contains /comments/ followed by a string). 这是行不通的,因为解码后的资源名称现在具有路径定界符和其中的查询字符串,这导致我的API中的路由变得混乱(我的路由配置假定请求路径包含/comments/后跟一个字符串)。

I could double-encode them or using some other encoding scheme than URI encode, but then that would add complexity to the clients, which I'm trying to avoid. 我可以对它们进行双重编码,也可以使用URI编码以外的其他编码方案进行编码,但这会增加客户端的复杂性,而我试图避免这种情况。

I have two specific questions: 我有两个具体问题:

  1. Is my URI design something I should continue working with or is there a better (best?) practice for doing what I'm trying to do? 我的URI设计是我应该继续使用的东西吗?或者是否有更好的(最佳方法)实践来做我想做的事情?

  2. I'm serving API requests with a Go process implemented using Martini 'microframework'. 我通过使用Martini'microframework'实现的Go流程满足API请求。 Is there something Go or Martini specific that I should do to make the URI-encoded resource names to stay encoded? 我需要执行Go或Martini特定的操作来使URI编码的资源名称保持编码状态吗? Perhaps a way to hint to the routing subsystem that the resource name is not just a string but a URL-encoded string? 也许可以向路由子系统暗示资源名称不仅仅是字符串而是URL编码的字符串的方法吗?

I don't know about your url scheme for your application, but single % encoded values are valid in a url in place of the chars they represent, and should be decoded by the server, what you are seeing is what I would expect. 我不知道您的应用程序使用的url方案,但是单个%编码值在url中代替它们表示的字符有效,并且应该由服务器解码,您所看到的就是我所期望的。 If you need to pass url reserved characters as a value and not have them decoded as part of the url, you will need to double % encode them. 如果您需要将url保留字符作为值传递,并且不将其解码为url的一部分,则需要对它们进行%编码的两倍。 It's a fairly common practice, the complexity added to the client & server will not be that much, and a short comment will do rightly. 这是相当普遍的做法,添加到客户端和服务器上的复杂性不会那么高,简短的注释是正确的。

In short, If you need to pass url chars, double % encode them, it's fine. 简而言之,如果您需要传递url字符,请对它们进行双重%编码,就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM