Parsing parameters with special characters in Ruby Grape

Feb 15, 2019

Recently I was building an API where I had to accept URLs as parameters in Grape routes. I URL-encoded the URL, but either my request would hit the wrong route, or it wouldn't capture anything after the second . in the URL, because it tries to parse that as a format ( .json for instance).

This is how I ended up fixing the problem:

resource :bookmarks do
    params do
      requires :url, type: String
    end
    get 'check/:url', requirements: { url: /[\s\S]*/ } do
      params[:url]
    end
end

The requirements part is the important bit. We're using Regex to capture any string after check/ in the URL.

Asad Dhamani

I'm a techie and entrepreneur who loves solving problems that people and businesses of our generation face through the use of technology. I also love coffee, astronomy, music, & traveling.

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.