Coder Social home page Coder Social logo

Comments (16)

mhenrixon avatar mhenrixon commented on May 29, 2024 1

@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.

Neil-Aframe avatar Neil-Aframe commented on May 29, 2024 1

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.

dblock avatar dblock commented on May 29, 2024

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.

bonescity avatar bonescity commented on May 29, 2024

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.

psynix avatar psynix commented on May 29, 2024

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

from grape.

mhenrixon avatar mhenrixon commented on May 29, 2024

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.

bobbytables avatar bobbytables commented on May 29, 2024

@mhenrixon Why not do it in the before filter?

from grape.

gertig avatar gertig commented on May 29, 2024

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

from grape.

sillylogger avatar sillylogger commented on May 29, 2024

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.

semarco avatar semarco commented on May 29, 2024

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.

outerim avatar outerim commented on May 29, 2024

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.

gertig avatar gertig commented on May 29, 2024

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.

bobbytables avatar bobbytables commented on May 29, 2024

#180

from grape.

Neil-Aframe avatar Neil-Aframe commented on May 29, 2024

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

from grape.

dblock avatar dblock commented on May 29, 2024

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.

Neil-Aframe avatar Neil-Aframe commented on May 29, 2024

repro = reproduction ?

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

from grape.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.