我很少看到关于REST最佳实践的文章,他们建议使用belo进行多列排序。获取/users?sort_by=-last_modified,+电子邮件
https://www.moesif.com/blog/technical/api-design/REST-API-Design-Filtering-Sorting-and-Pagination/
当我使用这种方法时,我看到-工作得很好,但是+被空格所取代。
一个快速的谷歌表示+是一个特殊的字符之后?在URL中。我在这里错过了什么?
> The following characters have special meaning in the path component of
> your URL (the path component is everything before the '?'): ";" |
> "/" | "?"
>
> In addition to those, the following characters have special meaning in
> the query part of your URL (everything after '?'). Therefore, if they
> are after the '?' you need to escape them: ":" | "@" | "&" | "=" |
> "+" | "$" | ","
>
> For a more in-depth explanation, see the RFC.
发布于 2020-02-06 18:39:39
我在这里错过了什么?
大部分是历史。
U+002B (+)是URI上下文中的子标记,可以在查询部分自由使用;参见RFC 3986附录A。
但是在web上,查询数据的一个常见来源是HTML提交;当我们提交表单时,处理引擎从表单中收集键值对,并创建一个应用程序/x-www-表单-urlencoded字符序列,这将成为URI的查询。
由于这是一种常见的情况,web服务器框架中的查询解析器通常默认为在允许您的定制代码访问数据之前逆转编码。
这意味着,在您的web日志中,您将看到:
/users?sort_by=-last_modified,+email
因为这是您收到的URI,但是在参数映射中您会看到
"sort_by" = "-last_modified, email"
因为“表单数据”在你看之前就被解码了。
表单urlencoding中有一个显式步骤,它将任何空格(U+0020)替换为U+002B,而U+002B则是百分比编码。
若要检查是否发生了这种情况,请尝试执行以下请求:
GET /users?sort_by=-last_modified,%2Bemail
我希望您会发现,您正在寻找的加号现在出现在您的表单参数中:
"sort_by" = "-last_modified,+email"
https://stackoverflow.com/questions/60100050
复制相似问题