开发者

Resolving URI character encoding issues in Rails

开发者 https://www.devze.com 2023-01-11 15:31 出处:网络
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.

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.r开发者_开发技巧b: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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消