You need to sign in to do that
Don't have an account?
Knowledge lover
The Apex class does not appear to be using the correct endpoint
as to finish the trailhead module "Trailhead API", I was stuck at part 6 "Search Using the Trailhead API"
here is the code I wrote below. Any idea why I encountered "Challenge Not yet complete... here's what's wrong:
The Apex class does not appear to be using the correct endpoint."?
public class trailheadAPIChallenge {
public static void getTrail(){
// create a new HttpRequest object
HttpRequest req = new HttpRequest();
// get the current user's sessionId to use later as an access token for the Trailhead API Authorization
string sid = UserInfo.getSessionId();
//System.debug('session information ' + sid);
// extend timeout to 20 seconds for slow responses
req.setTimeout(20000);
// create new response object for collecting the callout results
HttpResponse res = new HttpResponse();
Http http = new Http();
// fill in the necessary settings for calling out to the Trailhead API:
req.setEndpoint('https://api.trailhead.salesforce.com/v1/search/trails');
req.setMethod('GET');
req.setHeader('Content-Type', 'application/json; charset=utf-8');
req.setHeader('X-API-Key', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); //cannot expose X-API-Key
req.setBody('{"status": "Active"}');
req.setHeader('Authorization', 'Bearer ' + sid);
// try to callout to the Trailhead API Rest API and if it doesn't work, catch the errors for debugging
try {
// send the http request and collect it in a response
res = http.send(req);
// serialize the resulting String into a map of name-value pairs (String-Object)
Map<String, Object> jsonMap = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
// get the total_count from the map
Integer totalCount = (Integer)jsonMap.get('total_count');
// makes sure these are true
System.assert(totalCount > 0);
System.assertEquals(200, res.getStatusCode());
// output to the logs in case you want to view the results
System.debug('request: '+ req.toString());
System.debug('response: '+ res.toString());
} catch(System.CalloutException e) {
System.debug('Callout error: '+ e);
System.debug(res.toString());
}
}
}
here is the code I wrote below. Any idea why I encountered "Challenge Not yet complete... here's what's wrong:
The Apex class does not appear to be using the correct endpoint."?
public class trailheadAPIChallenge {
public static void getTrail(){
// create a new HttpRequest object
HttpRequest req = new HttpRequest();
// get the current user's sessionId to use later as an access token for the Trailhead API Authorization
string sid = UserInfo.getSessionId();
//System.debug('session information ' + sid);
// extend timeout to 20 seconds for slow responses
req.setTimeout(20000);
// create new response object for collecting the callout results
HttpResponse res = new HttpResponse();
Http http = new Http();
// fill in the necessary settings for calling out to the Trailhead API:
req.setEndpoint('https://api.trailhead.salesforce.com/v1/search/trails');
req.setMethod('GET');
req.setHeader('Content-Type', 'application/json; charset=utf-8');
req.setHeader('X-API-Key', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); //cannot expose X-API-Key
req.setBody('{"status": "Active"}');
req.setHeader('Authorization', 'Bearer ' + sid);
// try to callout to the Trailhead API Rest API and if it doesn't work, catch the errors for debugging
try {
// send the http request and collect it in a response
res = http.send(req);
// serialize the resulting String into a map of name-value pairs (String-Object)
Map<String, Object> jsonMap = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
// get the total_count from the map
Integer totalCount = (Integer)jsonMap.get('total_count');
// makes sure these are true
System.assert(totalCount > 0);
System.assertEquals(200, res.getStatusCode());
// output to the logs in case you want to view the results
System.debug('request: '+ req.toString());
System.debug('response: '+ res.toString());
} catch(System.CalloutException e) {
System.debug('Callout error: '+ e);
System.debug(res.toString());
}
}
}
For more information visit the given link to know more about Trailhead API.
https://api.trailhead.salesforce.com/#/
Let me know if this helps :)
Thanks,
Amit Singh