Closed jerelmiller closed 8 years ago
After looking into this further, it appears that the default behavior in this gem is already correct in that it should accept an Accept
header that does not specify the json api media type. However, if the media type is there, it just needs to validate that it doesn't include media type parameters.
Here's a useful resource for this very topic.
https://github.com/elliotttf/jsonapi-headers/commit/db85f3215238014ff980cdad07fec7f6071042f2
+1
This actually validates the Accept header with 'GET' method. Not 'POST'
it 'returns 406 with invalid Accept: header' do
headers = {
'ACCEPT' => 'application/vnd.api+jsondummmy'
}
# post '/api/v1/subscriptions', json_body.to_json ,headers # <= returns 415
get '/api/v1/subscriptions', headers # <= returns 406
expect(response.status).to eq 406 # Not acceptable
end
Is this JSON-API specific?
For now working around for this is
within controller.rb
before_action :restrict_accept_header
def restrict_accept_header
render json: { msg: 'Accept-Header must be application/json' }, status: 406 unless request.headers['Accept'] =~ /application\/vnd\.api\+json/ # :not_acceptable
end
This is not JSON-API specific error message, but it works.
I noticed that the
ensure_correct_media_type
before_action
does not validate against an invalidAccept
header. It appears to only validate against theContent-Type
header.