This project is archived and is in readonly mode.

#536 ✓resolved
Mike Subelsky

Add MIME type "application/jsonrequest" for compliance with JSON spec

Reported by Mike Subelsky | July 2nd, 2008 @ 04:31 PM | in 2.x

Currently Rails only recognizes "application/json" as a valid MIME type for JSON requests, but according to the JSON spec the MIME type should be "application/jsonrequest".

This patch merely adds "application/jsonrequest" as a synonym for "application/json", and also reworks the JSON parsing test slightly so that alternate MIME types can be passed in.

Comments and changes to this ticket

  • Mike Subelsky

    Mike Subelsky July 2nd, 2008 @ 04:34 PM

    For some reason Lighthouse is not upload the patch as an attachment (in Firefox and Safari) so I am pasting it below.

    diff --git a/actionpack/lib/action_controller/mime_types.rb b/actionpack/lib/action_controller/mime_types.rb

    index 01a266d..2d7fba1 100644

    --- a/actionpack/lib/action_controller/mime_types.rb

    +++ b/actionpack/lib/action_controller/mime_types.rb

    @@ -17,4 +17,5 @@ Mime::Type.register "multipart/form-data", :multipart_form

    Mime::Type.register "application/x-www-form-urlencoded", :url_encoded_form

    1. http://www.ietf.org/rfc/rfc4627.txt

    -Mime::Type.register "application/json", :json, %w( text/x-json )

    \ No newline at end of file

    +# http://www.json.org/JSONRequest....

    +Mime::Type.register "application/json", :json, %w( text/x-json application/jsonrequest )

    \ No newline at end of file

    diff --git a/actionpack/test/controller/request_test.rb b/actionpack/test/controller/request_test.rb

    index 2bd489b..5d677ca 100644

    --- a/actionpack/test/controller/request_test.rb

    +++ b/actionpack/test/controller/request_test.rb

    @@ -909,15 +909,21 @@ class LegacyXmlParamsParsingTest < XmlParamsParsingTest

    end

    class JsonParamsParsingTest < Test::Unit::TestCase

    • def test_hash_params
    • person = parse_body({:person => {:name => "David"}}.to_json)[:person]
    • def test_hash_params_for_application_json
    • person = parse_body({:person => {:name => "David"}}.to_json,'application/json')[:person]
    • assert_kind_of Hash, person
    • assert_equal 'David', person['name']
    • end

    +

    • def test_hash_params_for_application_jsonrequest
    • person = parse_body({:person => {:name => "David"}}.to_json,'application/jsonrequest')[:person]

    assert_kind_of Hash, person

    assert_equal 'David', person['name']

    end

    private

    • def parse_body(body)
    • env = { 'CONTENT_TYPE' => 'application/json',
    • def parse_body(body, content_type)
    • env = { 'CONTENT_TYPE' => content_type,

    'CONTENT_LENGTH' => body.size.to_s }

    cgi = ActionController::Integration::Session::StubCGI.new(env, body)

    ActionController::CgiRequest.new(cgi).request_parameters

  • Mike Subelsky
  • Mike Subelsky
  • Mike Subelsky
  • Mike Subelsky

    Mike Subelsky July 2nd, 2008 @ 07:21 PM

    Updated patch: http://pastie.org/226332

    (file uploading and @@@ still not working for me, sorry to gum up the works)

  • Mike Subelsky

    Mike Subelsky July 2nd, 2008 @ 07:22 PM

    From 644571c993b9baf3ff73e0b423a924dcbc2ffeef Mon Sep 17 00:00:00 2001
    From: Mike Subelsky <github@mikeshop.net>
    Date: Wed, 2 Jul 2008 14:15:43 -0400
    Subject: [PATCH] Added application/jsonrequest as a synonym for application/json
    
    ---
     actionpack/lib/action_controller/mime_types.rb |    3 ++-
     actionpack/test/controller/request_test.rb     |   14 ++++++++++----
     2 files changed, 12 insertions(+), 5 deletions(-)
    
    diff --git a/actionpack/lib/action_controller/mime_types.rb b/actionpack/lib/action_controller/mime_types.rb
    index 01a266d..2d7fba1 100644
    --- a/actionpack/lib/action_controller/mime_types.rb
    +++ b/actionpack/lib/action_controller/mime_types.rb
    @@ -17,4 +17,5 @@ Mime::Type.register "multipart/form-data", :multipart_form
     Mime::Type.register "application/x-www-form-urlencoded", :url_encoded_form
     
     # http://www.ietf.org/rfc/rfc4627.txt
    -Mime::Type.register "application/json", :json, %w( text/x-json )
    \ No newline at end of file
    +# http://www.json.org/JSONRequest....
    +Mime::Type.register "application/json", :json, %w( text/x-json application/jsonrequest )
    \ No newline at end of file
    diff --git a/actionpack/test/controller/request_test.rb b/actionpack/test/controller/request_test.rb
    index 2bd489b..81794a5 100644
    --- a/actionpack/test/controller/request_test.rb
    +++ b/actionpack/test/controller/request_test.rb
    @@ -909,15 +909,21 @@ class LegacyXmlParamsParsingTest < XmlParamsParsingTest
     end
     
     class JsonParamsParsingTest < Test::Unit::TestCase
    -  def test_hash_params
    -    person = parse_body({:person => {:name => "David"}}.to_json)[:person]
    +  def test_hash_params_for_application_json
    +    person = parse_body({:person => {:name => "David"}}.to_json,'application/json')[:person]
    +    assert_kind_of Hash, person
    +    assert_equal 'David', person['name']
    +  end
    +
    +  def test_hash_params_for_application_jsonrequest
    +    person = parse_body({:person => {:name => "David"}}.to_json,'application/jsonrequest')[:person]    
         assert_kind_of Hash, person
         assert_equal 'David', person['name']
       end
     
       private
    -    def parse_body(body)
    -      env = { 'CONTENT_TYPE'   => 'application/json',
    +    def parse_body(body,content_type)
    +      env = { 'CONTENT_TYPE'   => content_type,
                   'CONTENT_LENGTH' => body.size.to_s }
           cgi = ActionController::Integration::Session::StubCGI.new(env, body)
           ActionController::CgiRequest.new(cgi).request_parameters
    -- 
    1.5.5.3
    
    
  • Rick
  • Rick

    Rick July 2nd, 2008 @ 07:27 PM

    • State changed from “new” to “open”
    • Assigned user set to “Rick”
    test_line_offset(RenderTest)
        [./test/controller/render_test.rb:247:in `test_line_offset'
         ./test/../lib/../../activesupport/lib/active_support/testing/setup_and_teardown.rb:91:in `__send__'
         ./test/../lib/../../activesupport/lib/active_support/testing/setup_and_teardown.rb:91:in `run']:
    <"1"> expected but was
    <"75">.
    
  • Repository

    Repository July 2nd, 2008 @ 07:41 PM

    • State changed from “open” to “resolved”

    (from [8f640c381d9d1b74f6a0fc3648c21da373661914]) Added application/jsonrequest as a synonym for application/json

    [#536 state:resolved]

    http://github.com/rails/rails/co...

  • Yehuda Katz

    Yehuda Katz July 3rd, 2008 @ 08:19 PM

    The "spec" referred to by the opener of this ticket is not the JSON spec. It is a proposal by Douglas Crockford, that, as far as I know, has not been submitted to any standards body.

    More important are the specs that have actually been accepted by standards bodies, which contradict this "spec".

    RFC 4627 (The application/json Media Type for JavaScript Object Notation) also written by Douglas Crockford, is a registered IANA mime-type.

    The HTTP spec defines the Accept header:

    "The Accept request-header field can be used to specify certain media types which are acceptable for the response."

    application/jsonrequest is not a media type. It is a hack to get around some perceived security issues. Additionally, JSONRequest has not been accepted by any major browser at the current time. Instead, Firefox and Safari are implementing the Access Control spec at the W3C (http://www.w3.org/TR/access-cont... and IE is implementing its own XDR spec (some elucidated at http://code.msdn.microsoft.com/R...).

    In other words, it's fine to add this synonym, but it's frankly not an Accept type that is currently in use, nor part of any accepted spec.

  • Mike Subelsky

    Mike Subelsky July 4th, 2008 @ 01:15 PM

    Yehuda, that makes a lot of sense. I didn't have a lot of JSON background before starting this project, but I am assuming that if I mistakenly put too much authority into Crockford's document, a few other people after me might as well.

  • Yehuda Katz

    Yehuda Katz July 4th, 2008 @ 09:45 PM

    @mike the beauty is that only browsers have to implement this. Since no browser implemented it, there's no risk of the bizzaro content-type being requested.

  • dawson17

    dawson17 November 28th, 2010 @ 11:52 PM

    • Importance changed from “” to “”

    Thanks for clarifying these things about mime application/json. It was interesting and helpful.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Pages