function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jonathan Osgood 3Jonathan Osgood 3 

trailhead Apex REST Callouts error: Return Value must be of type: System.HttpResponse

Hi Everyone,

Trying to complete the Rest Callouts challenge and getting this error on the last line: return strResponse;. Here is my code:
public class AnimalLocator {

    public static HttpResponse getAnimalNameById(Integer ID){
        Http http           = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/'+ID);
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        
        String strResponse = '';
        
        if(response.getStatusCode()==200){
        
            Map<String,Object> results = (Map<String,Object>) JSON.deserializeUntyped(response.getBody());
            
            Map<String,Object> animals = (Map<String,Object>) results.get('animals');
           
            system.debug('These are the animals:');
            
            strResponse = string.valueOf(animals.get('name'));
            
        }
        return strResponse;
    }
}

Thank you!
Best Answer chosen by Jonathan Osgood 3
DebasisDebasis
Ji Jonathan,

Your method getAnimalNameById need a return type of HttpResponse .
public static HttpResponse getAnimalNameById(Integer ID){

but you are returning string from te method.
 return strResponse;

I think as per your code, you need to return String instead of HttpResponse. please use below code and let me know if it works for you.
public class AnimalLocator {

    public static String getAnimalNameById(Integer ID){
        Http http           = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/'+ID);
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        
        String strResponse = '';
        
        if(response.getStatusCode()==200){
        
            Map<String,Object> results = (Map<String,Object>) JSON.deserializeUntyped(response.getBody());
            
            Map<String,Object> animals = (Map<String,Object>) results.get('animals');
           
            system.debug('These are the animals:');
            
            strResponse = string.valueOf(animals.get('name'));
            
        }
        return strResponse;
    }
}

As a common practice, if your question is answered, please choose 1 best answer.
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Debasis

All Answers

DebasisDebasis
Ji Jonathan,

Your method getAnimalNameById need a return type of HttpResponse .
public static HttpResponse getAnimalNameById(Integer ID){

but you are returning string from te method.
 return strResponse;

I think as per your code, you need to return String instead of HttpResponse. please use below code and let me know if it works for you.
public class AnimalLocator {

    public static String getAnimalNameById(Integer ID){
        Http http           = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/'+ID);
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        
        String strResponse = '';
        
        if(response.getStatusCode()==200){
        
            Map<String,Object> results = (Map<String,Object>) JSON.deserializeUntyped(response.getBody());
            
            Map<String,Object> animals = (Map<String,Object>) results.get('animals');
           
            system.debug('These are the animals:');
            
            strResponse = string.valueOf(animals.get('name'));
            
        }
        return strResponse;
    }
}

As a common practice, if your question is answered, please choose 1 best answer.
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Debasis
This was selected as the best answer
Jonathan Osgood 3Jonathan Osgood 3
Completely overlooked that, thank you!