question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Modify Request Body for a GET request

See original GitHub issue

We currently have custom code for modifying request body which works but I want to move to the standard ModifyRequestBodyGatewayFilterFactory.

We have a scenario where Spring Cloud Gateway received a GET request and we need call an internal GraphQL api with a POST and a request body.

ModifyRequestBodyGatewayFilterFactory works great if there is a body present in the original request but doesn’t apply the rewriteFunction when there is no body on GET requests.

I am using v2.1.2.RELEASE and I have hacked together a change to ModifyRequestBodyGatewayFilterFactory which works for me:

Mono<?> modifiedBody = serverRequest.bodyToMono(inClass)
        .defaultIfEmpty("") //added this line
        .flatMap(o -> config.rewriteFunction.apply(exchange, o));

I appreciate that the ModifyRequestBodyGatewayFilterFactory is beta, but would it be possible to support adding a body for GET requests in the future?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
spencergibbcommented, Jul 3, 2019

I’m not sure I like the edit.

I think I prefer this (closer to what was there prior to the edit).

.switchIfEmpty((Mono) config.rewriteFunction.apply(exchange, null)); //added this line

I don’t like the idea of having a body with an empty string. I also think it might need to be opt-in since it is very unusual.

0reactions
gberche-orangecommented, Oct 26, 2020

I’m not sure this exactly covers the use-case of this issue, but I worked around empty body in usage of modifyRequestBody by testing for null the original body and returning Mono.empty() when null body.

.modifyRequestBody(String.class, String.class,
							(webExchange, originalBody) -> {
								if (originalBody != null) {
                                                                       //modify body
                                                                       [...]
									return Mono.just(originalBody);
								} else {
									return Mono.empty();
								}
							})

and submitted https://github.com/spring-cloud/spring-cloud-gateway/pull/2000

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to modify request body - node.js - Stack Overflow
This should help you get started: addEventListener('fetch', event => { event.respondWith(handleRequest(event.request)) }) async function ...
Read more >
Modifying the Request Body - TIBCO Product Documentation
To modify the request body in the pre-processing stage employ the content source ... Get Content source from request. Get input stream from...
Read more >
How to change request body before sending request - Help
I need to change the request body data before sending to server. I'm loading data from a json file. Json file may have...
Read more >
Add a Request Body to a POST Request | API Connector
Enter a Request Body ... As part of a POST, PUT, or PATCH request, a data payload can be sent to the server...
Read more >
Issues - GitHub
how to modify request.body/form/query in middleware? ... request.state like request.state.username=username ? if ok , then how to get in ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found