You need to sign in to do that
Don't have an account?
Zator
Apex REST Callouts Challenge issue - invalid HTTP method: Get
I have completed this challege, and ran the unit test, and it passes. I have verified that the name value is being populated correctly and being compared appropriately. So, all should be good, right?
However, when I click the Check Challenge button, I get an error:
I can post my code if someone wants to verify it, but I don't think the issue is in the code as it appears to be working correctly. Has anyone seen this kind of thing before?
However, when I click the Check Challenge button, I get an error:
Challenge Not yet complete... here's what's wrong:
There was an unexpected error in your org which is preventing this assessment check from completing:
System.CalloutException: Invalid HTTP method: Get
There was an unexpected error in your org which is preventing this assessment check from completing:
System.CalloutException: Invalid HTTP method: Get
I can post my code if someone wants to verify it, but I don't think the issue is in the code as it appears to be working correctly. Has anyone seen this kind of thing before?
All Answers
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 the reqest is successful, parse the JSON response
string animalName;
if(response.getStatusCode() == 200) {
// Deserializes the JSON string into collections of primitive data types
Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
Map<String, Object> p = (Map<String, Object>)m.get('animal');
animalName = (String)p.get('name');
System.debug('Animal name from AnimalLocator class: ' + animalName);
}
return animalName;
}
}
@isTest
public class AnimalLocatorTest {
@isTest static void testAnimalLocator(){
Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());
String name = AnimalLocator.getAnimalNameById(2);
System.debug('Animal name from test class: ' + name);
System.assert(name == 'mockbear');
}
}
@isTest
global class AnimalLocatorMock implements HttpCalloutMock{
global HTTPResponse respond(HTTPRequest request){
HttpResponse response = new HttpResponse();
response.setHeader('Content-Type','application/json');
response.setBody('{"animal":{"id":2,"name":"mockbear","eats":"berries, campers, adam seligman","says":"yum yum"}}');
response.setStatusCode(200);
return response;
}
}
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 the reqest is successful, parse the JSON response
string animalName;
if(response.getStatusCode() == 200) {
// Deserializes the JSON string into collections of primitive data types
Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
Map<String, Object> p = (Map<String, Object>)m.get('animal');
animalName = (String)p.get('name');
System.debug('Animal name from AnimalLocator class: ' + animalName);
}
return animalName;
}
}
@isTest
public class AnimalLocatorTest {
@isTest static void testAnimalLocator(){
Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());
String name = AnimalLocator.getAnimalNameById(2);
System.debug('Animal name from test class: ' + name);
System.assert(name == 'mockbear');
}
}
@isTest
global class AnimalLocatorMock implements HttpCalloutMock{
global HTTPResponse respond(HTTPRequest request){
HttpResponse response = new HttpResponse();
response.setHeader('Content-Type','application/json');
response.setBody('{"animal":{"id":2,"name":"mockbear","eats":"berries, campers, adam seligman","says":"yum yum"}}');
response.setStatusCode(200);
return response;
}
}