开发者

How to avoid Encoding::CompatibilityError with ruby 1.9 and mechanize form submit?

开发者 https://www.devze.com 2023-01-23 04:00 出处:网络
Currently, I am testing a ruby script that uses mechanize with ruby 1.9.2 (Ubuntu Lucid packages). The first line sets following options:

Currently, I am testing a ruby script that uses mechanize with ruby 1.9.2 (Ubuntu Lucid packages).

The first line sets following options:

#!/usr/bin/env ruby -KU

Some mechanize post code triggers following exception:

/usr/lib/ruby/1.9.1/www/mechanize/form.rb:222:in `join': incompatible character encodings: ISO-8859-1 and ASCII-8BIT (Encoding::CompatibilityError)
    from /usr/lib/ruby/1.9.1/www/mechanize/form.rb:222:in `request_data'
    from /usr/lib/ruby/1.9.1/www/mechanize.rb:401:in `post_form'
    from /usr/lib/ruby/1.9.1/www/mechanize.rb:344:in `submit'
    from /usr/lib/ruby/1.9.1/www/mechanize/form.rb:130:in `submit'

Mechanize is used like that in the source program:

# f is a Mechanize::Page object
final = f.form('final')
  f.form('final') { |x|
f.form('final') { |x|
  x.user == user
  #开发者_运维问答 ... 
}
a = final.submit

The data that is filled in (e.g. user) is read from disk via File.new() objects and via Nokogiri-objects that parse on File objects.

Printing the data to console does not show obvious enconding problems.

Thus my question: How to I fix this encoding issue when posting form data using mechanize?

Do I have to tell mechanize what what character encoding to use for http POSTs?


Well, looks like a bug in the 0.9.3 version of mechanize (probably issue 25). Using the current git version of mechanize fixes the posting issues for me.

But a mechanize 1.0 version should include the fix as well.

0

精彩评论

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

关注公众号