Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

default_format not applied if content-type not supplied #1587

Open
csoreff opened this issue Mar 5, 2017 · 0 comments
Open

default_format not applied if content-type not supplied #1587

csoreff opened this issue Mar 5, 2017 · 0 comments
Labels

Comments

@csoreff
Copy link

csoreff commented Mar 5, 2017

Follow-up on old issue #407

format and default_format are set to :json yet when content-type is not supplied it doesn't parse correctly:

curl --include \
     --request POST \
   --data-binary "{
    \"email\": \"test@test.com\",
    \"password\": \"password\"
}" \
'localhost:3000/api/v1/login'

results in:

#<Hashie::Mash {
    "email": "test@test.com",
    "password": "password"
}=nil>

The test doesn't handle when content type isn't supplied at all, it's given as a blank string:

    it 'parses data in default format' do
      subject.post '/data' do
        { x: params[:x] }
      end
      post '/data', '{"x":42}', 'CONTENT_TYPE' => ''
      expect(last_response.status).to eq(201)
      expect(last_response.body).to eq('{"x":42}')
    end

When I remove that content type and post without it, it breaks without even making it to #read_rack_input and test fails.

These are both false in #read_body_input so it returns here:

(!request.form_data? || !request.media_type)

content_type/media_type default to "application/x-www-form-urlencoded" when content-type isn't supplied. And since request.media_type is "application/x-www-form-urlencoded" instead of nil, it wouldn't use the default_format here either even if we made it to this point:

from grape/lib/grape/middleware/formatter.rb #read_rack_input

fmt = mime_types[request.media_type] if request.media_type
fmt ||= options[:default_format]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants