stripping trailing slashes

105 views
Skip to first unread message

alex...@solovyov.net

unread,
Mar 9, 2015, 4:11:29 PM3/9/15
to pedesta...@googlegroups.com
Hello,

I have an API which I intend to rewrite in Pedestal, and it has one peculiar detail - all its routes end with a slash. While this may be not the best API in the world, I have to keep backward compatibility. And Pedestal generates regular expressions to match routes without trailing slashes.

I have two questions:

1) If it's possible to instruct Pedestal to respect my trailing slashes;
2) or, optionally, how do I strip trailing slashes from incoming requests, so that Pedestal will match them correctly. Can't figure out if I can do that with interceptors - it seems to return 404 before calling my interceptor.

Paul deGrandis

unread,
Mar 10, 2015, 7:29:16 AM3/10/15
to pedesta...@googlegroups.com, alex...@solovyov.net
In your route definition, do your routes have trailing slashes?

If you just want to remove the trailing slash, you can do that in an interceptor.
You might try adding it to your own interceptor stack in your service map, to ensure it always runs before Pedestal begins routing your request.

Someone on the list can certainly help you down that path if you have questions!

Cheers,
Paul

Alexander Solovyov

unread,
Mar 10, 2015, 12:54:49 PM3/10/15
to Paul deGrandis, pedesta...@googlegroups.com
Paul, yes, my routes have trailing slashes - but Pedestal ignores them and treats them like urls without trailing slashes. So we can say that's a bug. :) I guess I'll go "interceptor in the service map" way right now.

Linus, thanks for the link, I'll try that!


Alexander Solovyov

unread,
Mar 10, 2015, 1:05:53 PM3/10/15
to pedesta...@googlegroups.com
That worked and I'm happy for now. :)

(ic/defbefore strip-trailing
  [ctx]
  (update-in ctx [:request :path-info] string/replace #"/$" ""))

Not sure if the fact that urls with trailing slashes are treated like ones without them is a bug, I guess it is - but maybe that's a design decision?

martin...@googlemail.com

unread,
Nov 28, 2018, 4:19:58 PM11/28/18
to pedestal-users
This is a very old thread but it shows up relatively high on Google and I think redirecting should be the preferred approach this. This way there will be no duplicate content issues and clients will be informed via 301 Moved Permanently that the URL they requested isn't meant to be used anymore.

So here we go, an interceptor that does just that:

(def redirect-trailing-slash-interceptor
{:name ::redirect-trailing-slash
:leave (fn [ctx]
(let [uri (-> ctx :request :uri)]
(cond-> ctx
(.endsWith uri "/")
(assoc :response {:status 301
:headers {"Location" (subs uri 0 (dec (.length uri)))}}))))})

Reply all
Reply to author
Forward
0 new messages