0

I am using RestTemplate to call an AutoTask API. However I am getting an error that says: 500 Internal Server Error: [{"errors":["Unexpected character encountered while parsing value: %. Path '', line 0, position 0."]}]

Swagger UI screenshot of the API call (Successful)

Console Output (Error)

Code:

@GetMapping("/all-clients")
private String getAllClients() {
    
    String COMPANIES_API_URL_Prefix = "https://webservices14.autotask.net/ATServicesRest/V1.0/Companies/query?search=";
    String COMPANIES_API_URL_Suffix = "{\"filter\":[{\"op\":\"in\",\"field\":\"CompanyType\",\"value\":[1]}]}";
    
     try {
            HttpHeaders headers = new HttpHeaders();
            headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
            headers.set("ApiIntegrationCode", "HUCXSL....."); //values partially hidden as it is sensitive information
            headers.set("UserName", "fdfsf...."); //values partially hidden as it is sensitive information
            headers.set("Secret", "yR*42......"); //values partially hidden as it is sensitive information

            HttpEntity entity = new HttpEntity(headers);
           
            RestTemplate restTemplate = new RestTemplate();
            
            String url = COMPANIES_API_URL_Prefix+URLEncoder.encode(COMPANIES_API_URL_Suffix);
            
            
            System.out.println(entity);
            
            System.out.println(url);
            
            ResponseEntity<Company[]> response = restTemplate.exchange(url, HttpMethod.GET, entity, Company[].class);
            
            System.out.println("Result - status ("+ response.getStatusCode() + ") has body: " + response.hasBody());
        }
     
        catch (Exception exception) {
            
            System.out.println("** Exception: "+ exception.getMessage());
        }
     
     return "all_clients";
}
Osama A.Rehman
  • 912
  • 1
  • 6
  • 12
  • Spring is probably already encoding the URL for you, resulting in the json getting double-encoded. When the endpoint decodes the query, it it still encoded, hence the error. Try skipping the call to `URLEncoder.encode`. – PMah Jul 05 '21 at 15:13

1 Answers1

0

You are doing encoding the JSON payload also that casing the problem or try as below. Set the JSON payload in httpEntity.

HttpHeaders headers = new HttpHeaders();
        headers.set(HttpHeaders.ACCEPT, 
MediaType.APPLICATION_JSON_VALUE);
        headers.set("ApiIntegrationCode", "HUCXSL....."); //values 
partially hidden as it is sensitive information
        headers.set("UserName", "fdfsf...."); //values partially 
hidden as it is sensitive information
        headers.set("Secret", "yR*42......"); //values partially hidden as it is sensitive information

        HttpEntity entity = new HttpEntity(headers, 
COMPANIES_API_URL_Suffix);;
       
        RestTemplate restTemplate = new RestTemplate();
        
    String url=OMPANIES_API_URL_Prefix;

        ResponseEntity<Company[]> response = 
 restTemplate.exchange(url, HttpMethod.GET, entity, 
Company[].class);
        
S. Anushan
  • 728
  • 1
  • 6
  • 12