How to modify request / response body?
Is it possible?
You can use WebFilter, here the README:
- Filters can modify downstream HTTP Request and HTTP Response (Add/Remove Headers, Add/Remove Parameters, Rewrite Path, Set Path, Hystrix, etc…)
Here the example how to add request parameter:
.route("addrequestheader_route") .uri("http://localhost:8080") .predicate(path("/search")) .add(addRequestParameter("value1", "value2"))
or
spring: cloud: gateway: routes: - id: addrequestparameter_route uri: http://localhost:8080 predicates: - Path=/search filters: - AddRequestParameter=value1, value2
Thank you, but I found how to do that. I don't know whether WebFilter can be used here, but Spencer uses for it the GatewayFilter.
See the correct example in WeirdBob's closed issue.
25 нояб. 2017 г. 1:25 пользователь "Henrique Amaral" < [email protected]> написал:
You can use WebFilter, here the README:
- Filters can modify downstream HTTP Request and HTTP Response (Add/Remove Headers, Add/Remove Parameters, Rewrite Path, Set Path, Hystrix, etc…)
Here the example how to add request parameter:
.route("addrequestheader_route") .uri("http://localhost:8080") .predicate(path("/search")) .add(addRequestParameter("value1", "value2"))
or
spring: cloud: gateway: routes:
- id: addrequestparameter_route uri: http://localhost:8080 predicates:
- Path=/search filters:
- AddRequestParameter=value1, value2
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/spring-cloud/spring-cloud-gateway/issues/113#issuecomment-346902240, or mute the thread https://github.com/notifications/unsubscribe-auth/ACzyA1oVyB9YsVxA3R1D3gbCeJ3WZ-Pwks5s50JYgaJpZM4QgeKW .
I didn't get how to change the response body. I have a json in the body and I want to add a property to it.
Example config:
spring:
cloud:
gateway:
routes:
- id: test
uri: http://jsonplaceholder.typicode.com:80
predicates:
- Path=/**
filters:
- MyFilter
My custom filter:
@Component
class MyFilter : GatewayFilterFactory {
override fun apply(args: Tuple?): GatewayFilter {
return GatewayFilter { exchange, chain ->
// I want to add a property to json response body, say, "a": 123. How?
chain.filter(exchange)
}
}
}
I want to add a property to json response body, say, "a": 123. How?
Search an other closed issue by "body" keyword, there is an example from WeirdBob and my additional comment to it. You can also use an appropriate Decoder to get a map or the object you are interested in.
Thanks. I found the issue you said and it kinda works. The problem is that that filter works for every route (because I'm using @Component I guess), but I'd like to apply it to a specific route through the configuration file as above (.yml, and not through .route().filters()) . How can I do that?
If I remove the @Component and set:
filters:
- MyFilter
it says:
Caused by: java.lang.IllegalArgumentException: Unable to find GatewayFilterFactory with name MyFilter
See how Spencer does. There is some builder behind which is responsible for built-in filters creation. Do not use component, you see, it's another model of data flow, not old known MVC.
Ah, you write about configuration file. Sorry. I do not using this approach because of ľd prefer fine tuning. I didn't research this way, not sure.
Keep in mind, it's a quite young project...
Hey guys, is it possible to modify response and return it immediately? for example I'm adding authorization and I want to reject further filtering if user is unauthorized?
Look for an example in closed issues. There is an example from @WeirdBob (post filter implementation).
As I understood from the example this filter will be invoked after request will be processed. I need to return response immediately in case of invalid authentication.
@malinovskiy-alex see https://github.com/spring-cloud/spring-cloud-gateway/blob/master/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/RequestRateLimiterGatewayFilterFactory.java#L69-L70 for how short circuit the request.
Hey @spencergibb Thanks for your answer. The main question for me here is how can I modify response body in such case.
@spencergibb Any thoughts on the question above? Thanks in advance
@malinovskiy-alex Why don't you just throw an exception in your filter? you can then implement a WebExceptionHandler to return the response you want (headers & body). Throwing an exception will stop the filtering chain.
That was one of the possible solutions, but I thought that there is some common way to update response body directly.
Also will this solution not brake reactive logic of the cloud api gateway?
I'm no expert but I'd say if you want to stop the filter chain, not route the request because of the authorization, the most logical thing to do is to throw an exception. It doesn't break the reactive logic, in the WebExceptionHandler you still have to return a Mono
Ok, thanks for your response. Let me check approach with webexceptionhandler.
It works. Thank you.