You need to sign in to do that
Don't have an account?
Amar Gadewar
Apex REST Callouts Challenge
In the Challenge it is requested to make a call to https://th-apex-http-callout.herokuapp.com/animals/:id.
Question is are they expecting a GET call or POST call. Most probably a GET call. But suppose integet pass to the method getAnimalNameById is "45678". How are we to formulate the URL.
Is it Option 1> replace id with value i.e https://th-apex-http-callout.herokuapp.com/animals/:45678
Option 2> pass using equal sign i.e https://th-apex-http-callout.herokuapp.com/animals/:id=45678.
Regardless i am getting the following response {"animal": {"id": 0,"name": "","eats": "","says": ""}}. I want some test data to verify the code is working does any body has that
Question is are they expecting a GET call or POST call. Most probably a GET call. But suppose integet pass to the method getAnimalNameById is "45678". How are we to formulate the URL.
Is it Option 1> replace id with value i.e https://th-apex-http-callout.herokuapp.com/animals/:45678
Option 2> pass using equal sign i.e https://th-apex-http-callout.herokuapp.com/animals/:id=45678.
Regardless i am getting the following response {"animal": {"id": 0,"name": "","eats": "","says": ""}}. I want some test data to verify the code is working does any body has that
which for me is returning the following data (if identifier == 1):
Still stuck on exactly how to parse that JSON, but hopefully this helps you move further.
All Answers
Global class AnimalLocator {
Global static String getAnimalNameById(Integer indentifier) {
String returnName ;
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/:id=' + indentifier);
request.setMethod('GET');
HttpResponse response = http.send(request);
// Parse the JSON response
if (response.getStatusCode() == 200) {
JSON2AnimalApex animal = JSON2AnimalApex.parse(response.getBody());
returnName = animal.animal.name;
}
return returnName;
}
}
which for me is returning the following data (if identifier == 1):
Still stuck on exactly how to parse that JSON, but hopefully this helps you move further.
Thanks for the tip on JSON2Apex. I did solve it a different way, in case anyone wants to try it later. Amar's solution is better though.
Challenge Not yet complete... here's what's wrong:
Executing the 'getAnimalNameById' method on 'AnimalLocator' failed. Make sure the method exists with the name 'getAnimalNameById', is public and static, accepts an Integer and returns a String.
I defined the method as below:
public class AnimalLocator {
public static String getAnimalNameById (Integer i)
Not sure, why i am seeing this error. can anyone help?
Did you return a string at the end of your method?
You can try this one:
@Febrian Tarigan 7: Thanks, it solved my problem too. The main reason is, in the challenge it is asked to pass id as the variable in endpoint URL. which is a keyword.
To parse the json I creaed a class as below using json to apex and some editing.
Then in the AnimalLocator class I used the above class to parse it as below.
You can see that I get the value of animal name using the line Hope this helps.
Any Help would be great, TIA.
## Animal Locator :
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);
/*Map<String,Object> results = (Map<String,Object>)JSON.deserializeUntyped(response.getBody());
system.debug('---->results'+results);
List<Object> animals = (List<Object>) results.get('animal');
system.debug('---->animal'+animals);*/
Map<Integer,String> mapAnimal = new Map<Integer,String>();
Integer varId;
String varName;
JSONParser parser1= JSON.createParser(response.getBody());
while (parser1.nextToken() != null) {
if ((parser1.getCurrentToken() == JSONToken.FIELD_NAME) && (parser1.getText() == 'id')) {
// Get the value.
parser1.nextToken();
// Fetch the ids for all animals in JSON Response.
varId=parser1.getIntegerValue();
System.debug('---->varId-->'+varID);
parser1.nextToken();
}
if ((parser1.getCurrentToken() == JSONToken.FIELD_NAME) && (parser1.getText() == 'name')) {
parser1.nextToken();
// Fetch the names for all animals in JSON Response.
varName=parser1.getText();
System.debug('---->varName-->'+varName);
}
mapAnimal.put(varId,varName);
}
system.debug('---->mapAnimal-->'+mapAnimal);
return mapAnimal.get(id);
}
}
## Mock Test :
@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('{"animal":[{"id":1,"name":"chicken","eats":"chicken food","says":"cluck cluck"},{"id":2,"name":"duck","eats":"worms","says":"pek pek"}]}');
response.setStatusCode(200);
return response;
}
}
## Test Class :
@isTest
private class AnimalLocatorTest {
@isTest static void testGetCallout() {
// Set mock callout class
Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());
// This causes a fake response to be sent
// from the class that implements HttpCalloutMock.
String response = AnimalLocator.getAnimalNameById(1);
system.debug('Test Response1--->'+response);
String expectedValue = 'chicken';
System.assertEquals(expectedValue,response);
String response2 = AnimalLocator.getAnimalNameById(2);
system.debug('Test Response2--->'+response2);
String expectedValue2 = 'duck';
System.assertEquals(expectedValue2,response2);
}
}