Problem with put requests?

670 views
Skip to first unread message

Josh Adams

unread,
Feb 19, 2012, 8:45:50 AM2/19/12
to Grape Framework Discussion
So I'm new to grape, and was putting together a backbone app with a
grape backend. GET requests cause me no grief, but I can't get a PUT
request to work to save my life.

The code is here: https://github.com/knewter/grape_demo. The app is
at /winelist.html

Anyway, I have this bit of code setting up the put request in my api:

desc "Update a wine"
put ':id' do
wine = Wine.find(id)
wine.update_attributes(params)
wine
end

I can see that the route exists:

ruby-1.9.2-p290 :002 > Acme::API.routes
=> [version=v1, method=GET, path=/api/:version/wines(.:format),
version=v1, method=GET, path=/api/:version/wines/:id(.:format),
version=v1, method=PUT, path=/api/:version/wines/:id(.:format)]

But when I PUT to that route, I get a 405 Method Not Allowed:

"NetworkError: 405 Method Not Allowed - http://localhost:9292/api/v1/wines/4f3fb4423aa9dc766a000001"

The grape log looks like this:

127.0.0.1 - - [19/Feb/2012 07:26:15] "PUT /api/v1/wines/
4f3fb4423aa9dc766a000001 HTTP/1.1" 405 19 0.0010

Anyway, I've spent like 3 hours trying to get past this (got to this
point very quickly, can't seem to get past it) so I figured it was
time to seek outside help. I've tried it in webrick, thin, and
unicorn. I grepped the grape codebase for 405 and it doesn't exist in
it. All of the grape tests pass for the SHA hash I'm on. Help?
Thanks...

Daniel Doubrovkine

unread,
Feb 19, 2012, 10:20:19 AM2/19/12
to ruby-...@googlegroups.com
I made a pull request with an implementation and specs here:  https://github.com/knewter/grape_demo/pull/1.

There were a few issues in the implementation, but not in routes. Maybe you just needed to bounce the Rack server? Grape doesn't get reloaded when code changes. Anyway, TDD often helps :)

cheers
dB.

Josh Adams

unread,
Feb 19, 2012, 4:26:57 PM2/19/12
to ruby-...@googlegroups.com
So it actually works in chrome but fails in firefox.  I'm deeply shamed that I spent 3 hours on it and never tested that.  I have no idea why it would be failing in firefox though :-\  irrelevant to my current toying about though, can figure that out later (inevitably related to headers or some such nonsense).

Thanks for the pull request.  I've been moving stuff around to separate files so I can't just pull it in, but I aim to merge it by hand :)

-Josh
--
Josh Adams
CTO | isotope|eleven
[cell] (205) 215-3957
[work] (877) 476-8671 x201

Josh Adams

unread,
Feb 19, 2012, 4:29:16 PM2/19/12
to ruby-...@googlegroups.com
I totally lied, pulls cleanly.  Forgot my changes were all on the backbone side. :-\

Josh Adams

unread,
Feb 19, 2012, 5:08:30 PM2/19/12
to ruby-...@googlegroups.com
Sorry to keep bugging people, just thought I'd follow up with what I found to be the problem ultimately.  I followed dB's tutorial on grape/rack/static pages here: code.dblock.org/grape-api-mounted-on-rack-w-static-pages

Somehow, using Rack::TryStatic causes the 405 Method Not Allowed problems.  If I pull it out, the API works fine.  This explains why the specs were passing when it was still throwing that error.  I'd love to figure it out, but the point is it was in no way a problem with grape :)

Thanks again guys.

-Josh

Daniel Doubrovkine

unread,
Feb 19, 2012, 9:15:20 PM2/19/12
to ruby-...@googlegroups.com
Josh,

If you have a repro for this in a test (maybe a nice Capybara integration test? :)), hit me up offline and I'd be happy to look at it.

cheers
dB.

Daniel Doubrovkine

unread,
Feb 20, 2012, 12:55:28 PM2/20/12
to ruby-...@googlegroups.com
Turns out Rack::File will throw 405s on any other method than GET.

I have a hacky-ish workaround in https://gist.github.com/1870297, which inspires itself from Rack::TryStatic, but I'd be curious to know how people that don't use Rails or Sinatra mount Grape with static pages?

Daniel Doubrovkine

unread,
Feb 21, 2012, 6:45:00 PM2/21/12
to ruby-...@googlegroups.com
I took a moment to rewrite this in a nicer way, came up with https://github.com/dblock/grape-on-rack/blob/master/app/acme_instance.rb. I got rid of TryStatic and used its good parts.

The demo in https://github.com/dblock/grape-on-rack/ now has 3 versions of an API, static pages, API tests and integration tests and clicks on a link with JQuery, doing a POST. 

cheers
dB.
Reply all
Reply to author
Forward
0 new messages