Reputation: 8326
below are my routes defined
- id: myId
uri: http://localhost:4010/
predicates:
- Path=/test/helloworld
- Method=POST
filters:
- ModifyUri
Based on the request , i want to change the uri so that i can forward the request to different host. I created a filter ModifyUri but its still forwarding the request to uri defined in my routes.
Here is sample
ServerHttpRequest.Builder builder = exchange.getRequest().mutate();
URI modifiedUri = URI.create("http://localhost:8999/new-path");
ServerHttpRequest modifiedRequest = builder
.uri(modifiedUri)
.build();
return chain.filter(exchange.mutate().request(modifiedRequest).build());
Upvotes: 0
Views: 1041
Reputation: 118
You can use changeRequestUri:
.route( "myId", r -> r.path( "/test/helloworld" )
//.filters( f -> f.filters( testFilter, filter )
//.stripPrefix( 1 )
.changeRequestUri( exchange -> {
//exchange.getResponse().getHeaders().add("X-Rate-Limit-Remaining", "0");
//String path = xchange.getRequest().getURI().getRawPath();
return Optional.of( URI.create( "$newUri + $path" ) );
})
).uri( "http://localhost:4010" ) )
Upvotes: 0
Reputation: 69
Not sure why you would want to change the uri. Normally, you define per route to which host you want to route to. Of course you can do a path rewrite, e.g.;
- id: myId
uri: http://localhost:4010/
predicates:
- Path=/test/helloworld
- Method=POST
filters:
- RewritePath=/test/helloworld, /new-path
But changing the routing target is not possible afaik. If you need to route to a different host, define a new route. The criteria to determine which route should match (e.g. headers, dates, query-parameters,…) are defined as predicates. Therefore, I really can’t imagine a use case where you would need to change the uri ad hoc. Which specific use case do you have in mind that can’t be solved with fillers and predicates? Maybe then it would be easier, to find a solution
Upvotes: -1