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

URI not working in WebClient [SPR-17162] #21698

Closed
spring-issuemaster opened this issue Aug 10, 2018 · 1 comment
Assignees
Labels

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Aug 10, 2018

yannxia opened SPR-17162 and commented

final URI uri = UriComponentsBuilder.fromUriString(destinationAddress).queryParams(serverHttpRequest.getQueryParams()).build().toUri();
        Mono<RepetitionReadClientResponse> clientResponse = webClient.method(Objects.requireNonNull(serverHttpRequest.getMethod()))
            .uri(uri)
            .headers(httpHeaders -> httpHeaders.putAll(serverHttpRequest.getHeaders()))
            .body(BodyInserters.fromPublisher(serverHttpRequest.getBody(), DataBuffer.class))
            .exchange()

if uri contains chinese character. then body will be empty....

another way.

final String uri = UriComponentsBuilder.fromUriString(destinationAddress).queryParams(serverHttpRequest.getQueryParams()).build().toUriString();
        log.debug("forward request to " + uri);

        Mono<RepetitionReadClientResponse> clientResponse = webClient.method(Objects.requireNonNull(serverHttpRequest.getMethod()))
            .uri(uri)
            .headers(httpHeaders -> httpHeaders.putAll(serverHttpRequest.getHeaders()))
            .body(BodyInserters.fromPublisher(serverHttpRequest.getBody(), DataBuffer.class))
            .exchange()
            .map(DefaultRepetitionReadClientResponse::new);

 it's be OK.

 uri is 

http://`host`:9032/test/chinese-param?a=1222121&b=中文

Affects: 5.0.3

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Aug 10, 2018

Rossen Stoyanchev commented

Is it the response body that's empty? Without knowing what the server does, I can't tell why.

One difference is that in the first example, toUri() invokes the java.net.URI constructor which encodes only illegal characters (e.g. space) but does not encode non-ASCII characters. So in the first example the URL remains "http://host:9032/test/chinese-param?a=1222121&b=中文".

In the second example, the input URL string is encoded transparently internally, within the WebClient, and becomes "http://localhost:9032/test/chinese-param?a=1222121&b=%E4%B8%AD%E6%96%87".

If this is what makes the difference, you can add an explicit call to encode:

final URI uri = UriComponentsBuilder
		.fromUriString(destinationAddress)
		.queryParams(serverHttpRequest.getQueryParams())
		.encode()
		.build()
		.toUri();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.