Nick
Nick

Reputation: 9051

Ktor: define route for URL with and without tailing slash

I am trying to learn Ktor, when responding to a GET request, I found that the trailing slash is treated as two routes, for example:

/greet?name=john
/greet/?name=john

Is it possible to define one route for the above two URLs, which is to handle trailing slash automatically?

Upvotes: 6

Views: 1124

Answers (3)

Shroud
Shroud

Reputation: 420

Be careful when using install(IgnoreTrailingSlash) with static content, as omitting the trailing slash will result in a base url without the final path segment.

For example, let's say I have something like this in my routing code...

staticResources("/static", "static")
get("/myendpoint")

And an html file, which imports some javascript...

<script type="module" src="static/index.js"></script>

The import will fail with the trailing slash.

Upvotes: 0

Hawt Sauce
Hawt Sauce

Reputation: 103

In your Application.module() or Application.configureRouting() add this:

install(IgnoreTrailingSlash)

Upvotes: 10

Aleksei Tirman
Aleksei Tirman

Reputation: 6999

You can use the IgnoreTrailingSlash plugin to solve your problem.

Upvotes: 6

Related Questions