Git Product home page Git Product logo

Comments (6)

aaronchi avatar aaronchi commented on May 12, 2024

This seems to work if you load Rack::PostBodyContentTypeParser from rack-contrib. Seems like something important to support

from grape.

drnic avatar drnic commented on May 12, 2024

Perhaps add a documentation patch to show how to include it?

from grape.

ajsharp avatar ajsharp commented on May 12, 2024

I've noticed this as well when sending POST requests from Safari, but everything works from Chrome. The only difference we can find is that the request content type header is set to "application/json; chartset=UTF-8" but in Chrome it's just "application/json". Going to fork and write a spec to see if this is actually the issue. Will post back results here.

from grape.

mhenrixon avatar mhenrixon commented on May 12, 2024

Why was this issue closed? I can't find any information about how to achieve posting JSON propertly to the Grape API

See this comment for more information

from grape.

aaronchi avatar aaronchi commented on May 12, 2024

I'm surprised this hasn't been addressed/fixed yet.

Here's how I hacked it in an old project:

# 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/
          env.update('rack.request.form_hash' => JSON.parse(env['rack.input'].read), 'rack.request.form_input' => env['rack.input'])
        end
        logger.info "#{env['REQUEST_METHOD']} #{env['PATH_INFO']}"
        route_set.freeze.call(env)
      end

    end
  end
end

from grape.

mhenrixon avatar mhenrixon commented on May 12, 2024

@aaronchi thanks a bunch for that. So much cleaner than what I am doing now for every put or post.

I had to change a tiny thing the code provided by @aaronchi doesn't work for get and delete requests

# 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

Having to go through this makes me sick. But works for now, something like this should probably be implemented in the head branch but I am unsure about what to test for etc. Some guidance would be nice.

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.