响应POST请求时出现意外的ASCII-8BIT编码

时间:2014-05-08 10:24:26

标签: ruby-on-rails ruby encoding utf-8 nginx

我的rails应用程序中有以下API方法:

 # -*- encoding : utf-8 -*-
 def statistics
    respond_to do |format|
      format.json {
        render json: Lead.scope1.scope2, root: false
      }
    end
 end

标准的ActiveModel :: Serializer for Lead模型:

# -*- encoding : utf-8 -*-
class LeadSerializer < ActiveModel::Serializer
  attributes :id, :external_id, :external_ip, :fio, :phone, :city, :region, :lead_status, :spl_status, :description, :created_at, :traffic_source, :ad_company, :amount, :product_id

  def attributes
    data = super
    data[:phone] = Digest::SHA256.base64digest(object.phone.to_s).truncate(15)
    data
  end

end

我的config.encoding = "utf-8"

中有application.rb
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8

在我的environment.rb中。我还在utf-8中指定了默认编码nginx.conf

当我从生产服务器上的rails控制台调用Lead.scope1.scope2.to_json时,响应的编码为UTF-8

但由于某些原因,当我尝试使用statistics从ruby脚本调用curb方法时,编码为ASCII-8BIT。这是脚本。

# encoding: utf-8
require 'curb'
curl = Curl::Easy.http_post("#{uri}",
             Curl::PostField.content( ...**... ),
       ) do |curl|
           curl.ssl_verify_peer = false
           curl.ssl_verify_host = false
end

curl.perform

p curl.body.encoding

#=> #<Encoding:ASCII-8BIT>

我该如何解决这个问题?

0 个答案:

没有答案