I just followed a trailhead on callouts and I am trying to get some values from an endpoint. My debug shows me my status is ok but I get an error on line 17:
Callout error Invalid conversion from runtime type List to Map
Anonymouse window
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('http://api.sandbox.test.com/v1/agencies');
request.setMethod('GET');
request.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
request.setHeader('Content-Type', 'application/json');
request.setHeader('X-test-APIKEY', 'xxxxxxxxxxxxxx');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
System.debug('OK');
// Deserialize 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 'agencies' key as a list
List<Object> agencies = (List<Object>) results.get('agencies');
System.debug('Received the following:');
for (Object agency: agencies) {
System.debug(agency);
}
}
My result should be similar to below
[
{
"uid": "72352a55-5be4-496e-804a-fc3kl261f0a4",
"name": "Agency 1"
},
{
"uid": "62352a55-5487-496e-804a-fc3klf48f0a4",
"name": "Agency 2"
}
]