Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider to add ability to modify the URI on ClientRequest.Builder [SPR-16093] #20642

Closed
spring-issuemaster opened this issue Oct 22, 2017 · 1 comment

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

commented Oct 22, 2017

Ruslan Stelmachenko opened SPR-16093 and commented

There is convinient method ClientRequest.from(ClientRequest other) which returns ClientRequest.Builder instance.

It is useful to create a request copy in ExchangeFilterFunction to modify request in some way. For example, to add some heasers.

Actually, this is used in ExchangeFilterFunctions.basicAuthentication() helper.

It works fine, but sometimes you need to modify not only headers, but also request URI (to add sign query parameter for example).

Now we forced to manually copy each request part to the new request just to change the URL:

private ExchangeFilterFunction addSignFilter() {
	return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
		URI url = UriComponentsBuilder.fromUri(clientRequest.url())
				.queryParam("sign", createSign(clientRequest))
				.build()
				.toUri();
		ClientRequest authenticatedRequest = ClientRequest.method(clientRequest.method(), url)
				.headers(headers -> headers.addAll(clientRequest.headers()))
				.cookies(cookies -> cookies.addAll(clientRequest.cookies()))
				.attributes(attributes -> attributes.putAll(clientRequest.attributes()))
				.body(clientRequest.body())
				.build();

		return Mono.just(authenticatedRequest);
	});
}

But if request builder had the url(URI url) setter, then we were able to do:

private ExchangeFilterFunction addSignFilter() {
	return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
		URI url = UriComponentsBuilder.fromUri(clientRequest.url())
				.queryParam("sign", createSign(clientRequest))
				.build()
				.toUri();
		ClientRequest authenticatedRequest = ClientRequest.from(clientRequest)
				.url(url)
				.build();

		return Mono.just(authenticatedRequest);
	});
}

This is much less error-prone when we want to change just one part of the request (URL in this case), than copying each request part by hand when creating new builder instance manually or using ClientRequest.method(HttpMethod method, URI url).


Affects: 5.0 GA

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Oct 23, 2017

Arjen Poutsma commented

Fixed in 40a6fba, for both URL as well as HttpMethod.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.