我是否需要对作为 POST 表单参数传递的字符串(例如 URL)进行编码

发布于 2024-08-06 11:40:16 字数 155 浏览 2 评论 0原文

我是否需要对作为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

审判长 2024-08-13 11:40:17

我需要对作为 POST 表单参数传递的字符串(例如 URL)进行编码吗?

这取决于您用来创建/发送 POST 请求的内容。如果您自己直接创建请求正文,那么您必须对每个参数进行 URL 编码:

POST / HTTP/1.1
Content-Type: application/x-www-form-urlencoded

foo=bar&url=http://www.example.com/url?innerparameter1=1&innerparameter2=2

这不好:innerparameter2 实际上是外部表单编码字符串的参数。它需要编码,看起来像:

foo=bar&url=http%3A//www.example.com/url%3Finnerparameter1%3D1%26innerparameter2%3D2

但是,如果您使用更高级别的东西来发出 POST 请求,并传入某种参数字符串的映射,我希望该组件能够处理 URL-为您编码。

代码?

Do I need to encode strings (eg URL) I pass as a POST form parameter?

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:

POST / HTTP/1.1
Content-Type: application/x-www-form-urlencoded

foo=bar&url=http://www.example.com/url?innerparameter1=1&innerparameter2=2

this is no good:innerparameter2 is actually a parameter of the outer form-encoded string. It would need encoding, which would look like:

foo=bar&url=http%3A//www.example.com/url%3Finnerparameter1%3D1%26innerparameter2%3D2

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?

遇见了你 2024-08-13 11:40:17

正如 bobince 提到的,您需要对作为 URL 参数传递的任何数据进行编码。通常,您使用的任何库都会处理这个问题。顺便说一句,这适用于所有 HTTP 请求。

例如,API 具有端点 GET /sites/:name
使用 cURL 它应该看起来像

curl http://example.com/sites/google%2Ecom

在 Ruby/Rails 中,您可以使用 URI.encodeURI.decode

>> URI.encode('google.com', /\W/)
"google%2Ecom"
>> URI.decode('google%2Ecom')
"google.com"

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

curl http://example.com/sites/google%2Ecom

In Ruby/Rails, you can use URI.encode and URI.decode:

>> URI.encode('google.com', /\W/)
"google%2Ecom"
>> URI.decode('google%2Ecom')
"google.com"
若水微香 2024-08-13 11:40:17

一般来说,如果您将编程或用户输入数据发送到 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文