解决 Rails 中的 URI 字符编码问题

发布于 2024-09-13 20:03:04 字数 1494 浏览 3 评论 0原文

我正在使用 Ruby on Rails 应用程序,该应用程序对第​​三方服务进行客户端 API 调用。然后,它从第 3 方 API 获取字符串并用它们生成 URI。

不幸的是,由于编码错误,Rails 无法解析某些 URI。我尝试通过encodeURIComponent 运行字符串,但这并不能解决问题。

这是我收到的错误:

Processing ApplicationController#index (for 67.101.113.66 at 2010-08-12 23:24:55) [GET]
  Parameters: {"name"=>"Camilo Andr\xE9s \xD1ustes", "recipient_id"=>"279"}

ArgumentError (invalid byte sequence in US-ASCII):
  <internal:prelude>:8:in `synchronize'
  /home/heroku_rack/lib/static_assets.rb:9:in `call'
  /home/heroku_rack/lib/last_access.rb:25:in `call'
  /home/heroku_rack/lib/date_header.rb:14:in `call'
  thin (1.2.6) lib/thin/connection.rb:76:in `block in pre_process'
  thin (1.2.6) lib/thin/connection.rb:74:in `catch'
  thin (1.2.6) lib/thin/connection.rb:74:in `pre_process'
  thin (1.2.6) lib/thin/connection.rb:57:in `process'
  thin (1.2.6) lib/thin/connection.rb:42:in `receive_data'
  eventmachine (0.12.10) lib/eventmachine.rb:256:in `run_machine'
  eventmachine (0.12.10) lib/eventmachine.rb:256:in `run'
  thin (1.2.6) lib/thin/backends/base.rb:57:in `start'
  thin (1.2.6) lib/thin/server.rb:156:in `start'
  thin (1.2.6) lib/thin/controllers/controller.rb:80:in `start'
  thin (1.2.6) lib/thin/runner.rb:177:in `run_command'
  thin (1.2.6) lib/thin/runner.rb:143:in `run!'
  thin (1.2.6) bin/thin:6:in `<top (required)>'
  /usr/ruby1.9.1/bin/thin:19:in `load'

  /usr/ruby1.9.1/bin/thin:19:in `<main>'

如何正确处理这些字符串作为我的应用程序的输入?

I'm working with a Ruby on Rails application that makes a client side API call to a 3rd party service. It then takes strings from the 3rd party API and generates URIs with them.

Unfortunately, Rails fails to parse some of the URIs due to encoding errors. I have tried running the strings through encodeURIComponent, but this does not resolve the issue.

Here is the error I get:

Processing ApplicationController#index (for 67.101.113.66 at 2010-08-12 23:24:55) [GET]
  Parameters: {"name"=>"Camilo Andr\xE9s \xD1ustes", "recipient_id"=>"279"}

ArgumentError (invalid byte sequence in US-ASCII):
  <internal:prelude>:8:in `synchronize'
  /home/heroku_rack/lib/static_assets.rb:9:in `call'
  /home/heroku_rack/lib/last_access.rb:25:in `call'
  /home/heroku_rack/lib/date_header.rb:14:in `call'
  thin (1.2.6) lib/thin/connection.rb:76:in `block in pre_process'
  thin (1.2.6) lib/thin/connection.rb:74:in `catch'
  thin (1.2.6) lib/thin/connection.rb:74:in `pre_process'
  thin (1.2.6) lib/thin/connection.rb:57:in `process'
  thin (1.2.6) lib/thin/connection.rb:42:in `receive_data'
  eventmachine (0.12.10) lib/eventmachine.rb:256:in `run_machine'
  eventmachine (0.12.10) lib/eventmachine.rb:256:in `run'
  thin (1.2.6) lib/thin/backends/base.rb:57:in `start'
  thin (1.2.6) lib/thin/server.rb:156:in `start'
  thin (1.2.6) lib/thin/controllers/controller.rb:80:in `start'
  thin (1.2.6) lib/thin/runner.rb:177:in `run_command'
  thin (1.2.6) lib/thin/runner.rb:143:in `run!'
  thin (1.2.6) bin/thin:6:in `<top (required)>'
  /usr/ruby1.9.1/bin/thin:19:in `load'

  /usr/ruby1.9.1/bin/thin:19:in `<main>'

How do I properly handle these strings as input for my application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

江南月 2024-09-20 20:03:04

这对我有用: <%= u name -%>

每当我必须在我的视图中传递身份验证令牌时,我都会使用这种技术:
<%= u form_authenticity_token %>

由于以上内容适用于视图,如果您想要特定于 ruby​​:

require 'uri'
foo = "http://google.com?query=hello"

bad = URI.escape(foo)
good = URI.escape(foo, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))

bad_uri = "http://mysite.com?service=#{bad}&bar=blah"
good_uri = "http://mysite.com?service=#{good}&bar=blah"

puts bad_uri
# outputs "http://mysite.com?service=http://google.com?query=hello&bar=blah"

puts good_uri
# outputs "http://mysite.com?service=http%3A%2F%2Fgoogle.com%3Fquery%3Dhello&bar=blah"

希望这会有所帮助。

This works for me: <%= u name -%>

I use this technique everytime i have to pass on auth token around in my views:
<%= u form_authenticity_token %>

Since the above is for views, if you want ruby specific:

require 'uri'
foo = "http://google.com?query=hello"

bad = URI.escape(foo)
good = URI.escape(foo, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))

bad_uri = "http://mysite.com?service=#{bad}&bar=blah"
good_uri = "http://mysite.com?service=#{good}&bar=blah"

puts bad_uri
# outputs "http://mysite.com?service=http://google.com?query=hello&bar=blah"

puts good_uri
# outputs "http://mysite.com?service=http%3A%2F%2Fgoogle.com%3Fquery%3Dhello&bar=blah"

Hope this helps.

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