Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incompatible character encodings: UTF-8 and ASCII-8BIT #671

Open
GBH opened this issue Sep 5, 2019 · 3 comments
Open

incompatible character encodings: UTF-8 and ASCII-8BIT #671

GBH opened this issue Sep 5, 2019 · 3 comments

Comments

@GBH
Copy link

GBH commented Sep 5, 2019

When trying to create a POST with an attachment I'm running into this issue. Here's a simple example:

params = {
  name: "Gunter",
  attachment: attachment
}

Now, the attachment is a PDF document generate by prawnPDF. Its encoding is ASCII-8BIT. (I'm wrapping it in StringIO thingy just so I can do .read on it)

If I call HTTParty::Request::Body.new(params) everything is great.

However, if I change Gunter to Günter by introducing UTF-8 character I end up with incompatible character encodings: UTF-8 and ASCII-8BIT. Coming from here: https://github.com/jnunemaker/httparty/blob/master/lib/httparty/request/body.rb#L42

It's basically assumes that attachment's .read method will always return an UTF-8 encoded payload. Is that always true? What is a sensible work-around here?

Thanks!

@ishields
Copy link

ishields commented Aug 16, 2021

@GBH Did anyone find a work around for this? I am having this same issue uploading a jpg file, while at the same time sending a string that is in UTF-8.

Encoding::CompatibilityError (incompatible character encodings: UTF-8 and ASCII-8BIT):

@jakeonfire
Copy link

i would suggest you either not do that, or encode your own payloads to binary/ASCII-8BIT. e.g.

params = {
  name: "Günter".force_encoding(Encoding::BINARY),
  file: some_binary_file
}

then on the other end you'll have to do the reverse

name = "Günter"
> "Günter"
name = name.force_encoding(Encoding::BINARY)
> "G\xC3\xBCnter"
name.force_encoding(Encoding::UTF_8)
> "Günter"

@Paprikas
Copy link

@jakeonfire You saved my day. force_encoding(Encoding::BINARY) did the trick

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants