jimmy
jimmy

Reputation: 515

How do I send a get request with path variables and query parameters using RestTemplate?

I'm trying to send the following request but I'm getting a message that I have an "ambiguous URI path enconding".

Here's the Get request:

ResponseEntity<CarDetail> carDetails = restTemplate.exchange(
                                                    builder.toUriString(),
                                                    HttpMethod.GET,
                                                    requestEntity,
                                                    CarDetail.class,
                                                    carId, ownerId);

builder is like this:

UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
                .queryParam("from", "2019-04-12T10:15:51.000Z")
                .queryParam("to", "2019-04-14T10:15:51.000Z");

and url is like this:

http://cars.com/car/{carId}/owner/{ownerId}/period

I've been trying to find where's the problem but couldn't figure out, any help would be appreciated.


Update: the builder.toUriString() is returning this:

http://cars.com/car/%7BcarId%7D/owner/%7BownerId%7D/period?from=2019-04-12T10:15:51.000Z&to=2019-04-14T10:15:51.000Z

I think that the problem is with the %7B thing. Any suggestion?

Upvotes: 1

Views: 2987

Answers (1)

jimmy
jimmy

Reputation: 515

I fixed the problem using builder.buildAndExpand(carVariable).toUri()

The solution looks like this:

ResponseEntity<CarDetail> carDetails = restTemplate.exchange(
       builder.buildAndExpand(carVariable).toUri(),
       HttpMethod.GET,
       requestEntity,
       CarDetail.class);

Upvotes: 3

Related Questions