How do I get the URL of the current request?

When working with Racket web applications, sometimes you need to know the URL of the request that got your code where it is. To extract that information, use the request-uri function. This works regardless of whether you’re working with stateful or stateless servlets.

It’s necessary, of course, to have access to the current request. That may necessitate some re-engineering on your part, such as changing the signatures of various functions so that they take in the request as an argument.

An alternative approach is to use parameterize, which would allow you to keep the current URL available to any function that could potentially get invokved as part of handing a request.

One gotcha to keep in mind that request-uri returns a URL structure; the value isn’t a string. If you need that, consider using url->string.