Rails JSON request is not parsed correctly into post parameters

后端 未结 2 1957
小鲜肉
小鲜肉 2021-01-31 11:16

I\'m trying to debug a problem where Rails isn\'t decoding the JSON POST data.

The server logs show:

2011-12-14T06:44:44+00:00 app[web.2]: Started POST 
         


        
2条回答
  •  离开以前
    2021-01-31 11:47

    before_filter :fix_ie_params, only: [:create, :update]
    

    For Thin:

    def fix_ie_params
      if request.format == '*/*'
        # try to JSON decode in case of IE XDR
        begin
    
          params.merge! ActiveSupport::JSON.decode(request.body.string)
    
        rescue Exception=>e
          # todo: log exception
        end
      end
    end
    

    For Unicorn and Phusion Passenger:

    def fix_ie_params
      if request.format == '*/*'
        # try to JSON decode in case of IE XDR
        begin
    
          request.body.rewind
          params.merge! ActiveSupport::JSON.decode(request.body.read)
    
        rescue Exception=>e
          # todo: log exception
        end
      end
    end
    

提交回复
热议问题