Follow the specification to use the JSON api, the content type must be set to application/vnd.api+json
; however, setting the ContentType
in every action in the controller seems tedious (see code below).
def foo
response.headers['Content-Type'] = 'application/vnd.api+json'
# ...
end
Is there a better way to achive the same result? Since this is an API only app is this possible to set content type at application level?
You can use the before_action
directive:
class ApplicationController < ActionController::API
before_action :set_headers
def set_headers
response.headers['Content-Type'] = 'application/vnd.api+json'
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With