httparty
httparty copied to clipboard
incompatible character encodings: UTF-8 and ASCII-8BIT
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!
@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):
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"
@jakeonfire You saved my day. force_encoding(Encoding::BINARY)
did the trick