Search code examples
springkotlinhttprequestspring-cloud-gateway

How to get OriginalRequest from HTTP Request in ServerWebExchange


I am using Spring Cloud Gateway, and am trying to figure out how to get the "OriginalRequest" from the ServerWebExchange Request (so that I can use the URI).

I can see the OriginalRequest in the IntelliJ debugger (please see screenshot below). The issue that I am having is that it does not appear to be a public field (please see the last screenshot).

Do you know how I can access the OriginalRequest? Thanks a lot!

IntelliJ debugger Picture What is available from the Object


Solution

  • I was able to get the URI for the OriginalRequest from the ServerWebExchange by doing the following:

    serverWebExchange.getAttribute<LinkedHashSet<URI?>?>(ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR)?.firstOrNull()

    Credit to this Gist for showing me how to get it in Java:

    LinkedHashSet<URI> attr = serverWebExchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR);