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
ZatorZator 

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:
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

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?
 
Best Answer chosen by Zator
James LoghryJames Loghry
Believe it's a case of capitalization.  Try 'GET' instead of 'Get'.

All Answers

ZatorZator
Here is my code...

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;
    }
}
 
James LoghryJames Loghry
Believe it's a case of capitalization.  Try 'GET' instead of 'Get'.
This was selected as the best answer
ZatorZator
Yep...that did it.  Thanks, James.  Still, it is interesting to me that it ran okay and the test passed.
Naresh Babu 39Naresh Babu 39
Add the url to remote site settings and this works perfectly....

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;
    }
}