User Story
As a user, I want to retrieve a list of animals from a remote server by making an HTTP GET request using the getAnimals
method in the herokuapp
class.
User Story: Retrieve list of animals from the remote server
Description:
- Given that I am a user of the application
- When I call the
getAnimals
method in the herokuapp
class
- Then the application should send an HTTP GET request to the URL 'https://th-apex-http-callout.herokuapp.com/animals'
- And the application should receive a response from the server
- If the response status code is 200 (indicating a successful response)
- The application should deserialize the response body into a map object named
results
- The application should retrieve a list of animals from the
animals
key in the results
map
- The application should output a debug message indicating the animals received
- For each animal in the list of animals, the application should output a debug message containing the animal's details
Acceptance Criteria:
- The
getAnimals
method should send an HTTP GET request to the specified URL
- The application should deserialize the response body and retrieve the list of animals
- The application should output debug messages for the received animals
- The implementation should handle any exceptions or errors that may occur during the HTTP request or response handling
Code
public class herokuapp {
public static void getAnimals(){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
request.setMethod('GET');
HttpResponse response = http.send(request);
System.debug(response.getBody());
if(response.getStatusCode() == 200) {
Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
List<Object> animals = (List<Object>) results.get('animals');
System.debug('Received the following animals:');
for(Object animal: animals) {
System.debug(animal);
}
}
}
}