2

In the challenge, it is specified :

The Apex class must be called 'AnimalLocator', have a 'getAnimalNameById' method that accepts an Integer and returns a String.

and i cannot understand the line :

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);
        if (response.getStatusCode() == 200) {
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            System.debug(results);
            if(results.size() > 0){
                for (Object animal : results.values()) {
                    System.debug(animal);
                }
            }
        }
        return String.valueOf(response) ;
    }    
}

As the debug result, i get ->

DEBUG|{"animals": ["majestic badger", "fluffy bunny", "scary bear", "chicken", "mighty moose"]}

The expected result must be ->

{"animal":{"id":1,"name":"chicken","eats":"chicken food","says":"cluck cluck"}}

i am unable to understand the issue!

6
  • I think that your id variable must be null. Commented Aug 30, 2017 at 7:47
  • See json.org for JSON syntax.The response you are getting is an array of animal names. A REST service might return such an array when you haven't asked for a specific value by supplying an id so check the id value you are requesting.
    – Keith C
    Commented Aug 30, 2017 at 7:48
  • That's not the case! id is not null Commented Aug 30, 2017 at 7:48
  • when i browse to the url th-apex-http-callout.herokuapp.com/animals/2 the result is fine, but when i try to access from a callout, it isn't returning the expected result Commented Aug 30, 2017 at 7:50
  • try with request.setMethod('GET'); Commented Aug 30, 2017 at 7:57

2 Answers 2

2

There is no problem in your method. It is working fine.

As @Martin & @Keith mentioned in the comments, you must check for Id is not null. and make sure you are passing Integer as param to this method while calling it.

    public static String getAnimalNameById( Integer id ){
        if( id != null ){
            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);
            // If the request is successful, parse the JSON response.
            if (response.getStatusCode() == 200) {
                // Deserializes the JSON string into collections of primitive data types.
                Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
                // Cast the values in the 'animal' key as a object
                Object animal = (Object) results.get('animal');
                System.debug('Received the following animal:');
                Map<String, Object> details = ( Map<String, Object> ) animal;
                String name = String.valueOf( details.get('name') );
                return name;
            }
        }
        return '';
    }

and call it as:

System.debug( '----> '+AnimalLocator.getAnimalNameById(1) );

It will get you the animal name as "chicken".

USER_DEBUG|[113]|DEBUG|----> chicken
1

Thankyou @thatherahere The issue was here:

In the Mock i mentioned

@isTest
global class AnimalLocatorMock implements HttpCalloutMock {
    // Implement this interface method
    global HTTPResponse respond(HTTPRequest request) {
        // Create a fake response
        HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
        response.setBody('{"animals": ["majestic badger", "fluffy bunny", "scary bear", "chicken", "mighty moose"]}');
        response.setStatusCode(200);
        return response; 
    }
}

Instead, it should be

response.setBody('{"animal":{"id":2,"name":"bear","eats":"berries, campers, adam seligman","says":"yum yum"}}');

I have been placing the wrong text.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .