lib/flapjack/gateways/jsonapi.rb in flapjack-1.3.0 vs lib/flapjack/gateways/jsonapi.rb in flapjack-1.4.0rc1
- old
+ new
@@ -33,13 +33,13 @@
include Flapjack::Utility
JSON_REQUEST_MIME_TYPES = ['application/vnd.api+json', 'application/json', 'application/json-patch+json']
# http://www.iana.org/assignments/media-types/application/vnd.api+json
- JSONAPI_MEDIA_TYPE = 'application/vnd.api+json; charset=utf-8'
+ JSONAPI_MEDIA_TYPE = 'application/vnd.api+json'
# http://tools.ietf.org/html/rfc6902
- JSON_PATCH_MEDIA_TYPE = 'application/json-patch+json; charset=utf-8'
+ JSON_PATCH_MEDIA_TYPE = 'application/json-patch+json'
class ContactNotFound < RuntimeError
attr_reader :contact_id
def initialize(contact_id)
@contact_id = contact_id
@@ -136,11 +136,11 @@
"#{request_info[:path_info]}#{query_string}")
end
headers = if 'DELETE'.eql?(request_info[:request_method])
# not set by default for delete, but the error structure is JSON
- {'Content-Type' => JSONAPI_MEDIA_TYPE}
+ {'Content-Type' => "#{JSONAPI_MEDIA_TYPE}; charset=#{Encoding.default_external}"}
else
{}
end
[status, headers, response_body]
@@ -279,10 +279,14 @@
msg_str = msg.join(", ")
logger.info "Error: #{msg_str}"
[status, {}, Flapjack.dump_json(:errors => msg)]
end
+ def charset_for_content_type(ct)
+ "#{ct}; charset=#{Encoding.default_external}"
+ end
+
def is_json_request?
Flapjack::Gateways::JSONAPI::JSON_REQUEST_MIME_TYPES.include?(request.content_type.split(/\s*[;,]\s*/, 2).first)
end
def is_jsonapi_request?
@@ -402,26 +406,26 @@
204
end
# The following catch-all routes act as impromptu filters for their method types
get '*' do
- content_type JSONAPI_MEDIA_TYPE
+ content_type charset_for_content_type(JSONAPI_MEDIA_TYPE)
cors_headers
pass
end
# bare 'params' may have splat/captures for regex route, see
# https://github.com/sinatra/sinatra/issues/453
post '*' do
halt(405) unless request.params.empty? || is_json_request? || is_jsonapi_request?
- content_type JSONAPI_MEDIA_TYPE
+ content_type charset_for_content_type(JSONAPI_MEDIA_TYPE)
cors_headers
pass
end
patch '*' do
halt(405) unless is_jsonpatch_request?
- content_type JSONAPI_MEDIA_TYPE
+ content_type charset_for_content_type(JSONAPI_MEDIA_TYPE)
cors_headers
pass
end
delete '*' do