我是否需要对作为 POST 表单参数传递的字符串(例如 URL)进行编码
我是否需要对作为 POST 表单参数传递的字符串(例如 URL)进行编码?
即,我想将我拥有的 URL 作为表单参数之一传递给我的 Web 应用程序(ruby on Rails)。那么 URL/URI 中是否存在需要编码的潜在字符?或者也许 Rails 无论如何都会处理这个问题?
Do I need to encode strings (eg URL) I pass as a POST form parameter?
Ie I want to pass a URL I have to my web application (ruby on rails) as one of the form parameters. So are there any potential characters in a URL/URI that would need to be encoded? Or perhaps rails would handle this anyway?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这取决于您用来创建/发送 POST 请求的内容。如果您自己直接创建请求正文,那么您必须对每个参数进行 URL 编码:
这不好:
innerparameter2
实际上是外部表单编码字符串的参数。它需要编码,看起来像:但是,如果您使用更高级别的东西来发出 POST 请求,并传入某种参数字符串的映射,我希望该组件能够处理 URL-为您编码。
代码?
That depends on what you're using to create/send your POST request. If you're directly creating the request body yourself, then yes you would have to URL-encode each parameter:
this is no good:
innerparameter2
is actually a parameter of the outer form-encoded string. It would need encoding, which would look like:If, however, you are using something higher-level to make the POST request, and passing in some kind of mapping of parameter strings, I would expect that component to take care of the URL-encoding for you.
Code?
正如 bobince 提到的,您需要对作为 URL 参数传递的任何数据进行编码。通常,您使用的任何库都会处理这个问题。顺便说一句,这适用于所有 HTTP 请求。
例如,API 具有端点
GET /sites/:name
。使用 cURL 它应该看起来像
在 Ruby/Rails 中,您可以使用
URI.encode
和URI.decode
:As bobince mentions, you need to encode any data that you're passing as URL parameters. Often whatever library you're using will take care of this. This applies to all HTTP requests BTW.
For example, an API has an endpoint
GET /sites/:name
.Using cURL it should look like
In Ruby/Rails, you can use
URI.encode
andURI.decode
:一般来说,如果您将编程或用户输入数据发送到 HTML 页面,则应该将其编码为 HTML。请记住,URL 通常包含 &字符并且应该对其进行编码,即使浏览器看起来可以正常处理它。
我不是 Ruby 人,所以我不知道如何在 Ruby 中做到这一点,也不熟悉 Ruby on Rails 来判断它是否会做到这一点(尽管我对此会有点惊讶),但是我建议的指南不是特定于语言的。
As a general statement, if you emit programmatic or user input data to an HTML page, you should encode it for HTML. Bear in mind that URLs often have the & character and that should be encoded, even if browsers appear to handle it okay.
I'm not a Ruby guy, so I don't know how you do that in Ruby, nor am I familiar with Ruby on Rails to say if it will do it (though I would be a little surprised by that), but the guideline I suggest isn't language specific.