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

Not loading params from JSON body #64

Closed
bonescity opened this issue Sep 22, 2011 · 16 comments
Closed

Not loading params from JSON body #64

bonescity opened this issue Sep 22, 2011 · 16 comments

Comments

@bonescity
Copy link

I'm using the most current version of Grape and when I try to POST with the content-type as "application/json," Grape isn't picking up the params in the JSON body. The only params listed when I debug is the version number.

@dblock
Copy link
Member

dblock commented Sep 22, 2011

Why would those appear as parameters? The request body is request.body. The JSON document can be as deep and complicated as you want, so it doesn't translate into a set of name/value pairs.

@bonescity
Copy link
Author

Sorry if I'm not explaining right. What I'm referring to is that in Rails, you can access a JSON element in the response body by using the params[:element] statement. It appears that this is broken in Grape because when I debug, the parameters are always empty.

I'm using Ruby 1.9.2 and Rails 3.1.

Thanks!

@psynix
Copy link

psynix commented Oct 23, 2011

You could always use the great Rack::Parser middleware.

@mhenrixon
Copy link

this is what I have to do with all my json requests:

authenticate!
hash = ActiveSupport::JSON.decode(request.body.read)
lowercasemodelname = current_user.has_many.create! hash['lowercasemodelname']
lowercasemodelname.as_json

This is not a very nice solution when I have to get my hands dirty like that.

This is how I post it:

POST /api/v1/:token/path HTTP/1.1
Accept: application/json
Content-Type: application/json

@bobbytables
Copy link
Contributor

@mhenrixon Why not do it in the before filter?

@mhenrixon
Copy link

@bobbytables I ended up opening up the Grape::API class with

# Overload grape to parse application/json objects until this is fixed
module Grape
  class API
    class << self

      def call(env)
        case env['CONTENT_TYPE']
        when /^application\/json/
          hash = env['rack.input'].read
          parsedForm = JSON.parse(hash) unless hash.blank?
          env.update('rack.request.form_hash' => parsedForm, 'rack.request.form_input' => env['rack.input']) if parsedForm
        end
        logger.info "#{env['REQUEST_METHOD']} #{env['PATH_INFO']}"
        route_set.freeze.call(env)
      end

    end
  end
end

@gertig
Copy link

gertig commented Jan 9, 2012

@mhenrixon Having the same problem as described above. Glad you found a somewhat workable solution for now.

@sillylogger
Copy link

I found this to be an annoyance as well. Fortunately I have my Grape app mounted under a route within a Rails application so one can merge in the params as parsed by the ActionDispatch::ParamsParser middleware. This can be done by monkey patching Grape::Endpoint#params as follows:

module Grape
  class Endpoint
    def params
      return @params if @params
      params = request.params.with_indifferent_access
      params.merge!(request.env['action_dispatch.request.request_parameters'] || {})
      params.merge!(request.env['rack.routing_args'] || {})
      @params = params
    end
  end
end

@semarco
Copy link

semarco commented Feb 21, 2012

Had a hard debugging session, to finally become illuminated and see that grape has issues with parsing the json request body .. thank you so much for your patches!

@outerim
Copy link

outerim commented May 25, 2012

My first time with Grape here. I've noticed this as well it's not so much a bug as it's just something that Grape doesn't appear to do for you. Coming from Rails this feels a little weird to be sure. Especially in something that seems to purport to be a tool for easily building JSON APIs. Wouldn't one naturally want to accept JSON formatted posts?

I'd consider throwing together a patch to handle this (without the Rails dependencies) if it's something the authors would ultimately like to incorporate but since this is months old with nothing going on I'm led to wonder.

@gertig
Copy link

gertig commented May 29, 2012

I'm on Grape 2.0 and I am getting the following error after doing an ajax POST with contentType of application/json

MultiJson::DecodeError (757: unexpected token at 'name=andrewgertig&body=hello'):

I am using @bobbytables solution and it seems to be choking on

env['rack.input'].read

@outerim, I think a solution to the JSON problem would be much appreciated by many of us

@bobbytables
Copy link
Contributor

#180

dblock added a commit that referenced this issue Jun 4, 2012
Add ability to get request bodies as parameters. Issue #64
@dblock dblock closed this as completed Jun 12, 2012
@Neil-Aframe
Copy link

This issue has re-surfaced in 1.2.6, perhaps due to re-factoring elsewhere. I'll raise a new issue

@dblock
Copy link
Member

dblock commented Jan 28, 2013

Are you looking at body_params? That was deleted. The parameters should be in the params, but you must make sure the request content-type is correct.

Please post a repro otherwise.

@Neil-Aframe
Copy link

In my tests I was sending header:

Content-Type: application/json

. . . to replicate the fault, and when I did so, I saw an obvious difference to params when inspecting env after I applied monkey patch as posted to #326.

@Neil-Aframe
Copy link

repro = reproduction ?

I'll try to do that later today - I should be able to manage that as rspec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants