Issue
So I have the ff. code to set headers and query parameters
public final RequestEntity<String> make(final Optional<String> cursor) {
final HttpHeaders headers = new HttpHeaders();
headers.add("Username", this.credentials.getUsername());
headers.add("Password", this.credentials.getPassword());
final UriComponentsBuilder body = UriComponentsBuilder.newInstance()
.queryParam("record_limit", this.recordLimit.toString())
.queryParam("cif", this.cif)
.queryParam("account_type", this.accountType)
.queryParam("account_status", this.accountStatus.getCode());
cursor.ifPresent(
cur -> body.queryParam("cursor", cur)
);
return new RequestEntity<>(
body.build().getQuery(),
headers,
HttpMethod.POST,
URI.create(this.url)
);
}
This code gives an error on my side since the query parameters are not being set properly, instead of a ? in the request URI, it has a , (comma) instead [see screenshot below]
Trying it on my curl/postman app with a comma, I could indeed replicate the error if I replace it with a comma from its intended query format with a ? (question mark), what do I do to fix this query params issue?. Thanks
Solution
UriComponentsBuilder
is a helper class to build a URI components. Don't use it to create request body.
You can create the URI either like this
final UriComponentsBuilder uriBuilder = UriComponentsBuilder
.newInstance()
.scheme("http")
.host("www.your-domain.com")
.path("/esb/service/CommunicatorGateway/customer/accountlist")
.queryParam("record_limit", this.recordLimit.toString())
.queryParam("cif", this.cif)
.queryParam("account_type", this.accountType)
.queryParam("account_status", this.accountStatus.getCode());
or like this,
final UriComponentsBuilder uriBuilder = UriComponentsBuilder
.fromHttpUrl("http://www.your-domain.com/esb/service/CommunicatorGateway/customer/accountlist")
.queryParam("record_limit", this.recordLimit.toString())
.queryParam("cif", this.cif)
.queryParam("account_type", this.accountType)
.queryParam("account_status", this.accountStatus.getCode());
Then finally create your RequestEntity
like this,
return new RequestEntity<>(
null,
headers,
HttpMethod.POST,
uriBuilder.build().encode().toUri()
);
Answered By - ray