4

I am working on a Spring Boot project using RestTemplate in order to perform a POST request toward an URL containing query parameter

This is my code:

@Override
public boolean insertNotaryDistrictDetailsAsPost(NotaryDistrictDetails notaryDistrict) {
    
    HttpHeaders basicAuthHeader =  this.getWpBasicAuthenticationHeader();
    HttpEntity<String> request = new HttpEntity<String>(basicAuthHeader);
    
    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(wpPortalWriteNotaryDistrictDetailsAsPostUrl)
            
            // BASIC INFO:
            .queryParam("title", notaryDistrict.getDistretto())
            .queryParam("wpcf-distretto-notary-district", notaryDistrict.getDistretto())
            .queryParam("wpcf-indirizzo-notary-district", notaryDistrict.getIndirizzo())
            .queryParam("wpcf-denominazione-notary-district", notaryDistrict.getDenominazione())
            .queryParam("wpcf-provincia-notary-district", notaryDistrict.getProvincia())
            .queryParam("wpcf-regione-notary-district", notaryDistrict.getRegione())
            .queryParam("wpcf-cap-notary-district", notaryDistrict.getCap())
            .queryParam("wpcf-telefono-notary-district", notaryDistrict.getTelefono())
            .queryParam("wpcf-fax-notary-district", notaryDistrict.getFax())
            .queryParam("wpcf-email-notary-district", notaryDistrict.getEmail())
            .queryParam("wpcf-pec-notary-district", notaryDistrict.getPec())
            .queryParam("wpcf-web-url-notary-district", notaryDistrict.getWebUrl());
    
            ResponseEntity<String> response = restTemplate.exchange(builder.toUriString(), 
                                                                    HttpMethod.POST, 
                                                                    request, 
                                                                    String.class);
            
            System.out.println("RESPONSE STATUS: " + response.getStatusCodeValue());
            System.out.println(response.getBody());

    return true;
}

The problem is that the generated URL is this one:

https://www.MYURL.it/wp-json/custom-api/notary-district?title=DISTRICT%20NAME%20TEST&wpcf-distretto-notary-district=DISTRICT%20NAME%20TEST&wpcf-indirizzo-notary-district=INDIRIZZO%20TEST&wpcf-denominazione-notary-district=DENOMINAZIONE%20DISTRETTO&wpcf-provincia-notary-district=PROVINCIA%20TEST&wpcf-regione-notary-district=REGIONE%20TEST&wpcf-cap-notary-district=00100&wpcf-telefono-notary-district=3293332232&wpcf-fax-notary-district=23232322&wpcf-email-notary-district=test@test.it&wpcf-pec-notary-district=pec@test.it&wpcf-web-url-notary-district=www.test.it

As you can see some query parameters contains white spaces encoded with %20. This is a problem because when it is received and saved from the called API, these fields are saved with %20 instead a white space. I can not change the called API in order to decode it as white space because was not developed by me.

Using Postman I have no problem: inserting the value of a query parameter containing white space it is received and saved with white space (not with encoded %20).

Exist a way to say to RestTemplate to use white spaces instead %20 characted?

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

1 Answers1

1
/**************************************** 
 * URL with white spaces
 ****************************************/
String url "https://www.MYURL.it/wp-json/custom-api/notary-district?title=DISTRICT NAME TEST"

// set encoded = false
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url);
URI uri = builder.build(false).toUri();

result = restTemplate.exchange( uri, HttpMethod.GET, entity, Class.class);



/**************************************** 
 * URL with %20
 ****************************************/
String url "https://www.MYURL.it/wp-json/custom-api/notary-district?title=DISTRICT%20NAME%20TEST"

// set encoded = true
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url);
URI uri = builder.build(true).toUri();

result = restTemplate.exchange( uri, HttpMethod.GET, entity, Class.class);
Jadson Santos
  • 199
  • 1
  • 2
  • 11