Parsing parameters with special characters in Ruby Grape
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.