-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Comments
Why would those appear as parameters? The request body is |
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! |
You could always use the great Rack::Parser middleware. |
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:
|
@mhenrixon Why not do it in the before filter? |
@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 |
@mhenrixon Having the same problem as described above. Glad you found a somewhat workable solution for now. |
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 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 |
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! |
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. |
I'm on Grape 2.0 and I am getting the following error after doing an ajax POST with contentType of application/json
I am using @bobbytables solution and it seems to be choking on
@outerim, I think a solution to the JSON problem would be much appreciated by many of us |
Add ability to get request bodies as parameters. Issue #64
This issue has re-surfaced in 1.2.6, perhaps due to re-factoring elsewhere. I'll raise a new issue |
Are you looking at Please post a repro otherwise. |
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. |
repro = reproduction ? I'll try to do that later today - I should be able to manage that as rspec |
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.
The text was updated successfully, but these errors were encountered: