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
Mounika AllenkiMounika Allenki 

Challenge Not yet complete... There was an unexpected error which is preventing this assessment check from completing: System.CalloutException: Unauthorized endpoint, please check Setup->Security->Remote site settings. endpoint . Please help:

Please help!
Best Answer chosen by Mounika Allenki
Lokesh KumarLokesh Kumar
HI Mounika,

First You have to perform below steps in your trailhead Org where you are checking this module.

https://th-apex-http-callout.herokuapp.com
https://th-apex-soap-service.herokuapp.com


Authorize both of these endpoint URLs by following these steps.

1.From Setup, enter Remote Site Settings in the Quick Find box, then click Remote Site Settings.
2.Click New Remote Site.
3.For the remote site name, enter animals_http.
4.For the remote site URL, enter https://th-apex-http-callout.herokuapp.com. This URL authorizes all subfolders for the endpoint, like https://th-apex-http-callout.herokuapp.com/path1 and https://th-apex-http-callout.herokuapp.com/path2.
5.For the description, enter Trailhead animal service: HTTP.
6.Click Save & New.
7.For the second remote site name, enter animals_soap.
8.For the remote site URL, enter https://th-apex-soap-service.herokuapp.com.
9.For the description, enter Trailhead animal service: SOAP.
10.Click Save.


for code reference you can verify from the below working code.

AnimalLocator
 
public class AnimalLocator{

public static string getAnimalNameById(Integer id) {

Http http = new Http();
HttpRequest hreq = new HttpRequest();
hreq.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/'+id);
hreq.setMethod('GET');
HttpResponse res = http.send(hreq);
Map<String,Object> animals = new Map<String,Object>();

if (res.getStatusCode() == 200) {
Map<String,Object> results = (Map<String,Object>)JSON.deserializeUntyped(res.getBody());
animals = (Map<String,Object>)results.get('animal');
}

else{System.debug('The status code returned was not expected: ' + res.getStatusCode() + ' ' + res.getStatus());}

return (string)animals.get('name');
}
}

AnimalLocatorTest
 
@isTest
private class AnimalLocatorTest{

@isTest static void AnimalsCalloutsTest() {

Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());

String ALresult = AnimalLocator.getAnimalNameById(1);

String expectedValue = 'chicken';

System.assertEquals(ALresult, expectedValue);


}
}

AnimalLocatorMock
 
@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":1,"name":"chicken","eats":"chicken food","says":"cluck cluck"}}');
        //response.setBody('{"animal":{"id":1,"name":"chicken"}}');
        response.setStatusCode(200);
        return response;
    }

}

Please let me know if this help you !!

Thanks !!

All Answers

manasa chudamanimanasa chudamani
Hi Mounika,

I think the endpoint which u configured in remote site setting is incorrect.Try to check the url by going to Setup--> Security--> Remote Site Settings.

Best Regards,
Manasa.G
Lokesh KumarLokesh Kumar
HI Mounika,

First You have to perform below steps in your trailhead Org where you are checking this module.

https://th-apex-http-callout.herokuapp.com
https://th-apex-soap-service.herokuapp.com


Authorize both of these endpoint URLs by following these steps.

1.From Setup, enter Remote Site Settings in the Quick Find box, then click Remote Site Settings.
2.Click New Remote Site.
3.For the remote site name, enter animals_http.
4.For the remote site URL, enter https://th-apex-http-callout.herokuapp.com. This URL authorizes all subfolders for the endpoint, like https://th-apex-http-callout.herokuapp.com/path1 and https://th-apex-http-callout.herokuapp.com/path2.
5.For the description, enter Trailhead animal service: HTTP.
6.Click Save & New.
7.For the second remote site name, enter animals_soap.
8.For the remote site URL, enter https://th-apex-soap-service.herokuapp.com.
9.For the description, enter Trailhead animal service: SOAP.
10.Click Save.


for code reference you can verify from the below working code.

AnimalLocator
 
public class AnimalLocator{

public static string getAnimalNameById(Integer id) {

Http http = new Http();
HttpRequest hreq = new HttpRequest();
hreq.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/'+id);
hreq.setMethod('GET');
HttpResponse res = http.send(hreq);
Map<String,Object> animals = new Map<String,Object>();

if (res.getStatusCode() == 200) {
Map<String,Object> results = (Map<String,Object>)JSON.deserializeUntyped(res.getBody());
animals = (Map<String,Object>)results.get('animal');
}

else{System.debug('The status code returned was not expected: ' + res.getStatusCode() + ' ' + res.getStatus());}

return (string)animals.get('name');
}
}

AnimalLocatorTest
 
@isTest
private class AnimalLocatorTest{

@isTest static void AnimalsCalloutsTest() {

Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());

String ALresult = AnimalLocator.getAnimalNameById(1);

String expectedValue = 'chicken';

System.assertEquals(ALresult, expectedValue);


}
}

AnimalLocatorMock
 
@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":1,"name":"chicken","eats":"chicken food","says":"cluck cluck"}}');
        //response.setBody('{"animal":{"id":1,"name":"chicken"}}');
        response.setStatusCode(200);
        return response;
    }

}

Please let me know if this help you !!

Thanks !!
This was selected as the best answer
vivek patel 37vivek patel 37
Thanks Lokesh Kumar...it was very helpful