Comments (16)
@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
from grape.
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.
from grape.
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.
from grape.
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!
from grape.
You could always use the great Rack::Parser middleware.
from grape.
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
from grape.
@mhenrixon Why not do it in the before filter?
from grape.
@mhenrixon Having the same problem as described above. Glad you found a somewhat workable solution for now.
from grape.
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
from grape.
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!
from grape.
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.
from grape.
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
from grape.
from grape.
This issue has re-surfaced in 1.2.6, perhaps due to re-factoring elsewhere. I'll raise a new issue
from grape.
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.
from grape.
repro = reproduction ?
I'll try to do that later today - I should be able to manage that as rspec
from grape.
Related Issues (20)
- Define contract instead of params HOT 8
- undefined method `rewind' for #<Rack::Lint::Wrapper::InputWrapper w/Rack >= 3.0 HOT 4
- custom type not suppot multiple types, raise `Grape::Exceptions::ValidationErrors` HOT 1
- Route#options and Route#settings are confusing HOT 1
- Ruby 3.3 && cookiejar = ArgumentError HOT 1
- Use Rack's HeadersHash instead of {}
- endpoint method_missing vs override inspect HOT 2
- Values Validator => Proc's arity < 2 ? HOT 3
- Rails Edge requires Ruby >= 3.1
- Fix specs for Rack Edge
- Fix `params` warning method redefined
- Better tracking of gems deprecation
- Rack::Lint::Error => a header value must be a String or Array of Strings, but the value of 'content-type' is a NilClass HOT 2
- Recognize_path should account for HTTP method HOT 1
- Grape and Protecting Against Mass Assignment Abuse HOT 8
- Why do you convert PATH_INFO? Is there some RFC? HOT 2
- Add support for exclusive parameter groups (e.g. exactly_one_of) HOT 2
- Drop support rack ~> 1 ? HOT 2
- Replace Appraisals by eval_gemfile ? HOT 3
- DeprecatedConstantProxy without a deprecator is deprecated HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from grape.