gpt4 book ai didi

Spring REST URL编码方案: %20 or + Which one?

转载 作者:行者123 更新时间:2023-12-03 04:47:17 24 4
gpt4 key购买 nike

我制作了一个 Spring REST 应用程序,您可以在其中基于 POST、PUT、GET、DELETE 的 HTTP 方法执行 CRUD 操作。我有典型的 URI 模板

http://host/root/{id}/{name}/{address} and etc.

我们有一个客户正在访问此 REST 服务。显然,他们正在以以下形式发送多字名称和地址的参数:

http://host/root/11/John+Smith/10+Las+Vegas+USA

他们使用基于 application/x-www-form-urlencoded 类型的 HTML 编码方案。根据维基百科的文章

The application/x-www-form-urlencoded type

The encoding used by default is based on a very early version of the general URI percent-encoding rules, with a number of modifications such as newline normalization and replacing spaces with "+" instead of "%20". - http://en.wikipedia.org/wiki/Percent-encoding

然而,标准 URL 编码方案似乎是使用 %20 来替换 URI 模板中的空格。哪一个是正确的?

我的 Spring REST 自动将 %20 转换为空格。它的解释是正确的。我正在使用 Spring 3.0.4。当我的 REST 服务满足 + 时,它会按原样接受。当然,当我将验证排除+时,它确实按照预期被排除了。

我符合标准吗还是存在双重标准?或者客户正在使用古老的方案?

最佳答案

重点是 application/x-www-form-urlencoded 只能仅在请求参数中使用,而路径中也支持百分比编码。

所以,

http://host/root/11/?name=John+Smith&address=10+Las+Vegas+USA

很好,并且会被 Spring MVC 正确解码,但是

http://host/root/11/John+Smith/10+Las+Vegas+USA

是错误的,Spring MVC 不会对其进行解码,因为应该使用以下形式:

http://host/root/11/John%20Smith/10%20Las%20Vegas%20USA

关于Spring REST URL编码方案: %20 or + Which one?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4170840/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com