Bkfsec
Bkfsec

Reputation: 347

How build a URL with UriComponentsBuilder where a parameter is a list of values

I am trying to build a URI with UriComponentsBuilder, I have a parameter where the value is a list.

UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl("https://example/endpoint")

                    .queryParam("client_id", clientId)
                    .queryParam("scope", list) //[scope1, scope2, scope3]
                    .queryParam("redirect_uri", redirectUri);

    return uriComponentsBuilder.build(false).encode().toUriString();

Output:

https://example/endpoint?client_id=clientId&scope=scope1&scope=scope2&scope=scope3&redirect_uri=https://redirect

This is not correct I have a scope parameter for each value of the list scope=scope1&scope=scope2&scope=scope3

The expected result should be:

 https://example/endpoint?client_id=clientId&scope=scope1+scope2+scope3&redirect_uri=https://redirect

What is the correct way to map list values to a parameter?

Upvotes: 0

Views: 1614

Answers (1)

azro
azro

Reputation: 54148

You may just do

.queryParam("scope", String.join("+",list))

Upvotes: 1

Related Questions