Krish
Krish

Reputation: 2002

How to get actual request URL in routed service when using Zuul service and Spring boot?

I am using zuul service as API gateway for all my micro services.

In zuul filter, I am able to get the requestUrl like below:

import javax.servlet.http.HttpServletRequest;
import com.netflix.zuul.context.RequestContext;
.....
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
String requestUrl = request.getRequestURL();

Here requestUrl: http://localhost:8052/api/userservice/users

In user service, when I am trying to get the request URL in spring boot rest controller using HttpServletRequest:

String requestUrl = request.getRequestURL();

Here requestUrl: http://localhost:8055/userservice/users

I am getting the routed service request url but NOT actual URL which client requested.

How to get actual request URL in routed service ?

Upvotes: 1

Views: 5405

Answers (2)

Azarea
Azarea

Reputation: 536

You are getting the routed service because when you call the getRequestURL method after the request url has been modified. Calling the same method in a pre filter which order is -50(for example), you can get the actual URL zuul server received.

So far I don't find any method to get the actual URL in a route/post filter after the request is modified, I think one way to solve this problem is define a pre filter to get the actual URL before modified, and put the url into a Threadlocal field, then you can get the url in the route/post filter by accessing the field.

Upvotes: 1

Mark Bramnik
Mark Bramnik

Reputation: 42491

Zuul creates a new Http Request to user service. obviously with different URL. So in the user service its impossible to get access to original request by design.

Zuul is supposed to encapsulate an original request, not reveal it. All headers, request parameters, request body should be preserved, it's only a path to be changed.

If for some reason there is a need to get parameters from original request, in Zuul filter they can be extracted and set as Headers to the "new" request before it's sent to user service. Then in UserService there should also be a filter that will "read" these headers and use the information as required

Upvotes: 0

Related Questions