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

POST and PUT request params get dropped (rails 4, angularjs) #417

Closed
hc5duke opened this issue Jun 4, 2013 · 7 comments
Closed

POST and PUT request params get dropped (rails 4, angularjs) #417

hc5duke opened this issue Jun 4, 2013 · 7 comments

Comments

@hc5duke
Copy link

hc5duke commented Jun 4, 2013

This is likely the same issue from this google groups thread. I created two small projects to demonstrate the issue:

3 vs 4

Both projects are using grape at 948696f on master with identical gems whenever possible. -rails3 is at v3.2.13, and -rails4 is at v4.0.0.rc1.

In both 3 and 4, jQuery requests are handled correctly, but when angular sends POST or PUT requests, the request is received by grape without any params (unless you hard-code it as part of the url), as evidenced by results fields. It is possible that the issue is angularjs - but given that the requests are nearly identical between what angularjs is sending between these two projects, it seems more likely that something in grape gem + rails 4 interaction is the cause of this bug.

@dblock
Copy link
Member

dblock commented Jun 4, 2013

Fyi, I upgraded https://github.com/dblock/grape-on-rails to Rails 4.0 rc1 without issues, everything works as expected. Looking.

@dblock
Copy link
Member

dblock commented Jun 4, 2013

I ran the app in https://github.com/hc5duke/grape_on_rails4 and I can do curl http://localhost:4000/api?data=x and get back "got x". So this is working as expected as far as I can see. How do I reproduce the problem?

@hc5duke
Copy link
Author

hc5duke commented Jun 4, 2013

Thanks for taking a look. I've done a little more poking around and narrowed it down to this:

Rails 3, working:

$ curl \
   --request POST grape-on-rails3.herokuapp.com/api \
   --verbose \
   --data    "{\"data\":\"text\"}"\
   --header  "Content-Type: application/json"
* About to connect() to grape-on-rails3.herokuapp.com port 80 (#0)
*   Trying 107.22.174.168... connected
* Connected to grape-on-rails3.herokuapp.com (107.22.174.168) port 80 (#0)
> POST /api HTTP/1.1
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5
> Host: grape-on-rails3.herokuapp.com
> Accept: */*
> Content-Type: application/json
> Content-Length: 23
> 
< HTTP/1.1 201 Created 
< Cache-Control: max-age=0, private, must-revalidate
< Content-Type: application/json
< Date: Tue, 04 Jun 2013 15:17:14 GMT
< Etag: "cd978d026d828c5ecb3786d585be8e74"
< Server: WEBrick/1.3.1 (Ruby/1.9.3/2013-05-15)
< X-Request-Id: 04e5cd6333f78eb16fbb0a28d6da624c
< X-Runtime: 0.254739
< X-Ua-Compatible: IE=Edge
< Content-Length: 21
< Connection: keep-alive
< 
* Connection #0 to host grape-on-rails3.herokuapp.com left intact
* Closing connection #0
"posted text"

Rails 4, not working:

$ curl \
   --request POST grape-on-rails4.herokuapp.com/api \
   --verbose \
   --data    "{\"data\":\"angular text\"}"\
   --header  "Content-Type: application/json"
* About to connect() to grape-on-rails4.herokuapp.com port 80 (#0)
*   Trying 184.73.160.229... connected
* Connected to grape-on-rails4.herokuapp.com (184.73.160.229) port 80 (#0)
> POST /api HTTP/1.1
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5
> Host: grape-on-rails4.herokuapp.com
> Accept: */*
> Content-Type: application/json
> Content-Length: 23
> 
< HTTP/1.1 201 Created 
< Cache-Control: max-age=0, private, must-revalidate
< Content-Type: application/json
< Date: Tue, 04 Jun 2013 15:16:58 GMT
< Etag: "f377b68c3170ed7bc6d8e6c5ce0cd4d3"
< Server: WEBrick/1.3.1 (Ruby/1.9.3/2013-05-15)
< X-Request-Id: 584286bf-08fc-4d5c-a89c-dd928c2d8197
< X-Runtime: 1.226634
< Content-Length: 9
< Connection: keep-alive
< 
* Connection #0 to host grape-on-rails4.herokuapp.com left intact
* Closing connection #0
"posted "

@dblock dblock closed this as completed in 37fe09a Jun 4, 2013
@dblock
Copy link
Member

dblock commented Jun 4, 2013

Rails forgets to rewind the input it reads. It's probably by design, I don't think I want to track that one down. Fixed in 37fe09a and also added a working sample with a test in dblock/grape-on-rails@b721e42.

This can be monkey patched like this if you're on a stable version of Grape (config/initializers/grape.rb).

 module Grape
  module Middleware
    class Formatter < Base
      alias_method :_read_body_input, :read_body_input
      def read_body_input
        env['rack.input'].rewind if env['rack.input']
        _read_body_input
      end
    end
  end
end

@hc5duke
Copy link
Author

hc5duke commented Jun 4, 2013

Sweet. Thanks for the quick turnaround on this.

@dblock
Copy link
Member

dblock commented Jun 4, 2013

Opened rails/rails#10840, maybe someone on the Rails team will comment.

@kesin
Copy link

kesin commented Oct 23, 2015

Works fine for me!

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

No branches or pull requests

3 participants