Remove charset from Rails content type

后端 未结 4 1021
轮回少年
轮回少年 2021-01-18 13:27

I have a old-stupid service making request to my app that fails when the Content-Type include the charset line

Content-Type    text/html; charset=utf-8


        
相关标签:
4条回答
  • 2021-01-18 13:48

    This worked for me:

    class MyController
    
      after_filter :remove_charset
    
      def remove_charset
        headers['Content-type'] = "text/html"
      end
    end
    

    If you're working on development, make sure you clear your browser's cache.

    There is this method, but didn't work for me. I don't know why, it may even be a bug.

    0 讨论(0)
  • 2021-01-18 13:48

    The only way I was able to get it to work is by setting the default charset

    ActionController::Base.default_charset = nil
    

    Also, setting the Content-Transfer-Encoding header to binary will turn off the charset.

    0 讨论(0)
  • 2021-01-18 13:48

    Putting this in the controller did it for me:

    ActionDispatch::Response::default_charset = nil
    

    I put in in my base controller to remove it from all responses.

    0 讨论(0)
  • 2021-01-18 13:56

    For Rails 3/4, the code that handles this is in ActionDispatch::Response.assign_default_content_type_and_charset! in actionpack/lib/action_dispatch/http/response.rb.

    Setting response.headers['Content-Type'] instead of response.content_type should eliminate the charset. Chubas' solution does this for all responses.


    For Rails 2, the code that handles this is in content_type= and charset= in actionpack/lib/action_controller/response.rb.

    As Carson's solution describes, setting ActionController::Base.default_charset = nil should eliminate the charset.

    0 讨论(0)
提交回复
热议问题