Rails: how to treat alternative Accept: content-types as JSON?

后端 未结 2 1117
鱼传尺愫
鱼传尺愫 2021-02-20 10:10

So far I\'ve found two ways for request.format.json? to be true in Rails (i.e. where the incoming request is treated as JSON). One is if you request a resource and

相关标签:
2条回答
  • 2021-02-20 10:41

    We move iPhone requests over to HTML in our app with a before_filter like so:

    before_filter :determine_format
    
    def determine_format
        request.format = :iphone if (request.env["HTTP_USER_AGENT"] =~ /iPhone/ && request.format == :html)
    end
    

    I imagine you can do something similar with your specific format, maybe like this:

    def determine_format
        request.format = :json if (request.format == 'application/vnd.myapp_v1+json')
    end
    
    0 讨论(0)
  • 2021-02-20 10:42

    An alternative to this is:

    api_mime_types = %W(
      application/vnd.api+json
      text/x-json
      application/json
    )
    
    Mime::Type.unregister :json
    Mime::Type.register 'application/json', :json, api_mime_types
    

    In initializers/mime_types.rb.

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