simsicon / weibo_2

A ruby gem for sina weibo oauth2 api, with examples, and it actually works.
MIT License
194 stars 86 forks source link

Encoding::CompatibilityError #8

Closed happypeter closed 11 years ago

happypeter commented 11 years ago
Encoding::CompatibilityError - incompatible character encodings: UTF-8 and US-ASCII:

After connected with my weibo account, I could get my lastest status, I could send weibo in both English and Chinese. But, when I tried to attach a image, the above error showed up, and it's 100% reproducible.

Is this a bug? or am I missing sth? plz help.

happypeter commented 11 years ago

BTW. The code I am running is just the example dir of this repo and I had to

-gem 'time-ago-in-words'
+gem 'time_ago_in_words'

to make it work.

$ ruby -v
ruby 1.9.3p125 
happypeter commented 11 years ago

error with context

119.80.191.71 - - [02/May/2013 14:42:04] "POST /update " 302 - 1.9850
119.80.191.71 - - [02/May/2013 14:42:08] "GET / " 200 13582 4.2787
119.80.191.71 - - [02/May/2013 14:42:08] "GET /screen.css " 200 256 0.0017
119.80.191.71 - - [02/May/2013 14:42:09] "GET /favicon.ico " 404 18 0.0016
Encoding::CompatibilityError - incompatible character encodings: UTF-8 and US-ASCII:
^I/home/peter/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/weibo_2-0.1.1/lib/weibo_2/api/v2/base.rb:48:in `block in build_multipart_bodies'
^I/home/peter/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/weibo_2-0.1.1/lib/weibo_2/api/v2/base.rb:42:in `each'
^I/home/peter/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/weibo_2-0.1.1/lib/weibo_2/api/v2/base.rb:42:in `build_multipart_bodies'
^I/home/peter/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/weibo_2-0.1.1/lib/weibo_2/api/v2/statuses.rb:102:in `upload'
^I/home/peter/apps/weibo/example.rb:77:in `block in <top (required)>'
simsicon commented 11 years ago

Hi, I am pretty sure it's because the example are using a older weibo_2 gem, I have fixed this issue, please pull the latest source to test, and thank you for mentioning gem time-ago-in-words has deprecated.

simsicon commented 11 years ago

Just FYI, please remember to bundle update.

git pull
cd example
bundle update

Let me know the result, so I can close this issue. Thanks.

happypeter commented 11 years ago

pulled down your lastest changes, do bundle update and restart the server, but NO LUCK, same error:

Encoding::CompatibilityError - incompatible character encodings: UTF-8 and US-ASCII:
^I/home/peter/.rbenv/versions/1.9.3-p125/lib/ruby/gems/1.9.1/gems/weibo_2-0.1.2/lib/weibo_2/api/v2/base.rb:48:in `block in build_multipart_bodies'

except that you can see the 0.1.2 version of the gem is in use now.

simsicon commented 11 years ago

I tried on 2.0.0-p0, 1.9.3-p374 and 1.9.3-p125, uploaded several images, not showing the error you are pointing out.

  1. Dose this happen to all image uploading? Or just happen to some specific image?
  2. Dose this image have a Chinese file name?
  3. If it's some file keeps failing, could you please send it to me so I can take a look? my email is simsicon#gmail.com. Thanks.
happypeter commented 11 years ago
  1. have tried with several img file with different format, error was the same
  2. NO
  3. you can download my github avatar and have a try :smile:
simsicon commented 11 years ago

I reproduced this problem, the reason causing it is that this gem uses mime-type multipart/form-data to post pictures, and when building the post body, image file is binary while other parts are UTF-8.

I have fixed with this commit, https://github.com/simsicon/weibo_2/commit/c98afd48a5125d1da6b717ce4149addaf18c0650

Oh, by the way, I moved the example to here, try it out.

Thank you.

happypeter commented 11 years ago

it worked! yeaaaaahhhhh...

A little problem:

there was a trailing string Content-Transfer-Encoding: binary showed up after the status body I actually sent.

simsicon commented 11 years ago

Yeah, working is cool.

The extra content-transfer-encoding is totally fine, I added it to satisfy http multipart post protocol.

Thanks.

happypeter commented 11 years ago

@simsicon Thanks for your work.

But

content-transfer-encoding is showed to all my audience, I don't think this is totally fine

simsicon commented 11 years ago

I did not get what you mean, I made a mistake, and it's fixed now, sorry for this. Please run

bundle update weibo_2
happypeter commented 11 years ago

Cool man, your fast response and kindness really get me impressed.