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

Period in URL? #158

Closed
gaiottino opened this issue Apr 3, 2012 · 11 comments
Closed

Period in URL? #158

gaiottino opened this issue Apr 3, 2012 · 11 comments

Comments

@gaiottino
Copy link
Contributor

Hi there,

Really liking Grape so far. I'm converting a Sinatra app to Grape and I've got a couple of questions:

  • get '/users/:email' doesn't work for email addresses. Grape is trying to change the format since I'm testing with a .com and I therefore get "The requested format is not supported." How can I allow periods in the URL?
  • post '/users/:email/activate' is not recognized as a valid route, I'm just getting "Not Found"

How can I solve these issues?

@jch
Copy link
Contributor

jch commented Apr 3, 2012

I wrote a quick sinatra app that shows you case, and Sinatra also does not handle parsing this case.

require 'sinatra'

# GET /users/foo@bar.com returns not found
get '/users/:email' do
  puts params[:email]
end

On the other hand, rails seems to handle this case. I'd be happy to merge a pull request if you'd like to investigate how Rails handles this case. In the meantime, if you change your route and specify the email as a query parameter instead of part of the route, it works as you'd expect

class API < Grape::API
  # GET /users?email=foo@bar.com
  get '/users' do
    puts params[:email]
  end
end

I might take a look this evening to see if there's an easy fix involved.

@gaiottino
Copy link
Contributor Author

Regarding your Sinatra example, I'm not sure what's going on. I had to try your simple example since in my code I inherit from Sinatra::Base and use some other gems thinking they may affect why it's working for me, but it still works?

require 'bundler/setup'
require 'sinatra'

get '/user/:email' do
  params[:email]
end

I've tried it in Chrome and FF on OSX and here's a curl from the terminal:

$ curl http://localhost:4567/users/foo@bar.com
foo@bar.com

It's not giving me a not found. Using Sinatra 1.3.2

@SegFaultAX
Copy link

I did some poking around in the Rails routing layer and it appears that they use the Journey Library for all their route parsing needs whereas Grape is relying on Rack::Mount directly. Would using Journey in Grape to achieve identical routing behavior be out of the question, or should we simply patch Rack to achieve the desired functionality?

Edit: Clarified dependency on Rack::Mount.

@jch
Copy link
Contributor

jch commented Apr 3, 2012

@SegFaultAX I heard about journey, but haven't dug into it at all since there's no real docs for it. My guess is that the Rack::Mount::Strexp is using a regexp that's too strict, but @mbleigh knows more about that part of the code. Using Journey would be nice since we'd be able to ride along with Rails, but I'm not sure what the level of effort for integrating would be.

@gaiottino
Copy link
Contributor Author

Saw that @rodzyn already made pull request regarding the same issue. #145. He also links to a question about param regex so it seems it would be a welcomed change.

@bobbytables
Copy link
Contributor

Perhaps this is where the magic is happening?

https://github.com/rails/journey/blob/master/lib/journey/router.rb#L125

@SegFaultAX
Copy link

@bobbytables That's where the route is actually being matched but unless I'm much mistaken what we need is to modify the generated regex used in that comparison to include the OP's desired functionality. That step happens when the endpoint is actually being built. In other words, the named capture group "foo" generated for path "/:foo" does not currently match valid email meta-characters (eg . and @). Does that sound about right @jch and @gaiottino?

@gaiottino
Copy link
Contributor Author

Correct

On Tuesday 3 April 2012 at 22:42, Michael-Keith Bernard wrote:

@bobbytables That's where the route is actually being matched but unless I'm much mistaken what we need is to modify the generated regex used in that comparison to include the OP's desired functionality. That step happens when the endpoint is actually being built. In other words, the named capture group "foo" generated for path "/:foo" does not currently match valid email meta-characters (eg . and @). Does that sound about right @jch and @gaiottino?


Reply to this email directly or view it on GitHub:
#158 (comment)

@gaiottino
Copy link
Contributor Author

I've simplified @rodzyn's work a bit and created my own pull request here: #159

Sry but was unable to find how to add the pull request to this issue.

@i3zhe
Copy link

i3zhe commented May 14, 2014

Hello, I read this and know the period issue was fixed by the PR #159 . But it doesn't seem to work if I add another param after the email. (I am using the latest Grape 0.7.0)

This is the sample code:

    get 'calendars/:email/:id' do
      "123"
    end

when I hit http://localhost:9393/v3/calendars/123@123.com/primary, it returns 404 error:
2014-05-14 4 13 01

If I remove the period from url, it returns 200 OK:
2014-05-14 4 13 23

Any thought?

@i3zhe
Copy link

i3zhe commented May 14, 2014

Oops, by checking the test on spec/grape/endpoint_spec.rb, I find the solution:

get 'calendars/:email/:id', :requirements => { :email => /YOUR_REGEXP_HERE/i } do
   "123"
end

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

5 participants