开发者

Charset and POST request

开发者 https://www.devze.com 2022-12-24 14:03 出处:网络
I have a Rails 2.3.5 application that is working fine with UTF-8 and international characters. Now I have made some integration to a payment gateway where I POST some data, wait a while and get a POST

I have a Rails 2.3.5 application that is working fine with UTF-8 and international characters. Now I have made some integration to a payment gateway where I POST some data, wait a while and get a POST back. The problem is that when I get that post back the international characters are broken. Instead of "sørensen" I get: "sørens开发者_JAVA技巧en". If I do an "iconv -t ISO-8859-1 -f UTF8" it gets correctly converted to the former (I do that from a OS X command prompt). I have examined the POST request with logger.info(request.headers.inspect) in my controller and I can see that no charset parameter is given.

The POST from the gateway should be ISO-8859-1 according to them - is the problem that Rails thinks that it is UTF8? Is that consistent with the issue I am seeing?

I know that one solution is to simply convert the params-hash with Iconv in the controller but I would like to know what is happening.

Thanks in advance.

Regards,

Jacob


can you verify that the request headers are set appropriately in the POST. you can see the acceptable values in the following link. http://en.wikipedia.org/wiki/List_of_HTTP_headers


I ended up doing this:

In the model:

FIELDS_TO_FIX=[:name, :city, :address]  

# DIBS sucks and sends UTF8 in some strange encoding. So we have to do an array.pack on
# strings when posting to DIBS and unpack them at callback.
#
def self.fix_encoding(params)  
  FIELDS_TO_FIX.each do |f|
    next if params[f].nil?
    params[f]=params[f].unpack("M")[0].force_encoding("UTF-8")
  end
  params
end

In the create action of the controller:

  params[:booking]=Booking.fix_encoding(params[:booking])

And in the view that posts to the external resource:

<input type="hidden" name="booking[name]" value="<%= [@booking.name].pack("M") %>" />

I hope it helps someone.

0

精彩评论

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