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
Dhinesh MuralidharanDhinesh Muralidharan 

Trialhead Apex REST Callouts challenge

Hi i am very new to salesforce and has statred learning through Trialhead. I am in the REST callouts section. I started it but i don't know how to get the response. i am not sure if i am going about this right. This is my first time seeing a http service. please any help or guidance would be appreciated. i have pasted my code here .. I get an error that an object cannot be assigned to a string. if i change the type of result to object , the error goes away.I am not sure how to convert it back to a string.

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);
        
        // check the response
      if (response.getStatusCode() == 200)
      {
           String result = JSON.deserializeUntyped(response.getBody());
           
      }//end of if
            return null;
    }//end of method
    }//end of class
Nagendra ChinchinadaNagendra Chinchinada
You have to parse it in this way,

Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody()); 
List<Object> animals = (List<Object>) results.get('animals');

 
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 animalName ;
        string animalEats ;
        string animalSays ;
        
        // check the response
      if (response.getStatusCode() == 200)
      {
	 // 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 'animals' key as a list
    List<Object> animals = (List<Object>) results.get('animals');
    for (Object animal: animals) {
     System.debug(animal);      
        
        //animalName = animal.name;
         //animalEats = animal.eats;
         //animalSays = animal.says;
        
        //system.debug(animalName +'  '+animalEats+'  '+animalSays);
           
      }//end of if
      }
            return null;
    }//end of method
    }//end of class

 
Dhinesh MuralidharanDhinesh Muralidharan
Hi Nagendra,
             I tried it this way but i got null values only. It does not seem to locate the animal by id. when i look at the debug logs it shows that the id is null. am i missing something. were you able to get the animal name by id.
Nagendra ChinchinadaNagendra Chinchinada
If we set end point like this then we will get,

request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/' + id);​
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 animalName ;
        string animalEats ;
        string animalSays ;
        
        // check the response
      if (response.getStatusCode() == 200)
      {
	 // 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 'animals' key as a list
    List<Object> animals = (List<Object>) results.get('animals');
    for (Object animal: animals) {
&nbsp;    System.debug(animal);      
        
        //animalName = animal.name;
         //animalEats = animal.eats;
         //animalSays = animal.says;
        
        //system.debug(animalName +'  '+animalEats+'  '+animalSays);
           
      }//end of if
      }
            return null;
    }//end of method
    }//end of class