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

prefix doesn't work for base path requests #196

Closed
adamgotterer opened this issue Jul 3, 2012 · 5 comments
Closed

prefix doesn't work for base path requests #196

adamgotterer opened this issue Jul 3, 2012 · 5 comments

Comments

@adamgotterer
Copy link
Contributor

require 'grape'

class MyApi < Grape::API
  prefix 'myapi'   

  get '/' do
    'Works!'
  end
end     

Making a GET request to /myapi will cause a 404.

It works as expected when using a resource block:

require 'grape'

class MyApi < Grape::API
  #prefix 'myapi'   

  resource :myapi do
    get '/' do
      'Works!'
    end
  end
end   
@tobert
Copy link

tobert commented Aug 2, 2012

I'm also seeing this behavior.

module Hastur
  module API
    class V1 < Grape::API
      include Hastur::TimeUtil # import all the usec_* methods
      include Hastur::API::Constants

      helpers Hastur::API::Helpers

      # cheeze the version to "api", next version will be "v2"
      prefix "api"
      format :json
      error_format :json
      rescue_from :all

      # does not return on /api with this version or get "/" do
      get do
        { 
          :type     => "#{root_uri}/api/type",
          :app      => "#{root_uri}/api/app",
          :node     => "#{root_uri}/api/node",
        }
      end
      # SNIP
    end
  end
end

@rhunter
Copy link
Contributor

rhunter commented Oct 2, 2012

I'm seeing the similar behaviour with Grape's "version using path" feature.

module MyThing
  class API < Grape::API
    version 'v1', :using => :path
    get '/' do
      {:expected => 'GET /v1/ should hit this method',
       :actual => 'GET /v1/ responds 404 Not Found'}
    end
  end
end

I guess "version using path" uses "prefix" under the hood. Namespaces work fine, though, and I guessed they use "prefix" too?

@dblock dblock closed this as completed in 25f8ec0 Jan 5, 2013
@dblock
Copy link
Member

dblock commented Jan 5, 2013

Fixed on HEAD. This was tricky.

A namespaced API call path looks like this in rack-mount: (?-mix:\A\/awesome\/sauce(?:\.(?<format>[^\/.?]+))?\Z), while a prefixed root call (?-mix:\A\/awesome\/sauce\/(?:\.(?<format>[^\/.?]+))?\Z). So what's the difference? The root had a trailing slash, which is not included in the matching algorithm when traversing the precompiled rack path tree. So a call to GET /awesome/sauce/ would not find anything in the latter case. The fix is to make sure there're no trailing slashes when mounting the API.

@therocketforever
Copy link

This seems to be an issue again.

No problem on 0.9.0, bundle update brings me to 0.10.0 then I see the same problem reported here. added a get '/test' route to a few endpoints to confirm.

seems prefix is not being respected on base paths using resource

I did find that setting the prefix value inside the route worked

this is not working

class Users < Grape::API
  prefix "api"
  resource :users do
    get do
      @users = { users: User.all }
    end

this works, but has to be done for each endpoint base segment =(

class Users < Grape::API
  resource :users do
    prefix "api"
    get do
      @users = { users: User.all }
    end

@dblock
Copy link
Member

dblock commented Dec 25, 2014

@therocketforever This is a different problem, I opened #867.

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

No branches or pull requests

5 participants