I'm testing this API and having this problem when I'm executing the callout from the a Dev Console Anonymous Window. Crazy thing is that I'm testing the endpoint from Postman and it's returning a valid JSON (checked), so I know the problem is me, but I can't figure out what I'm doing wrong 🙁
public class starWarsCallout {
@AuraEnabled
public static Map <String, Object> getCalloutResponse(String url){
HttpRequest req = new HttpRequest();
req.setHeader('Content-Type','application/json;charset=UTF-8');
req.setEndpoint(url);
req.setMethod('GET');
HTTPResponse res = new Http().send(req);
if (res.getStatusCode() >= 200 && res.getStatusCode() < 300) {
Map<String,Object> result = (Map<String,Object>) JSON.deserializeUntyped(res.getBody());
return result;
}
//Throw custom exception
return null;
}
}
This is in the Anonymous window:
starWarsCallout.getCalloutResponse('https://swapi.dev/api/people/1/');
and this is the crazy error that won't let me debug the error:
Unexpected character ('<' (code 60)): expected a valid value (number,
String, array, object, 'true', 'false' or 'null')
This is the JSON response from Postman in case it's needed, but you can check it out yourself here (https://swapi.dev/api/people/1/)
{
"name": "Luke Skywalker",
"height": "172",
"mass": "77",
"hair_color": "blond",
"skin_color": "fair",
"eye_color": "blue",
"birth_year": "19BBY",
"gender": "male",
"homeworld": "http://swapi.dev/api/planets/1/",
"films": [
"http://swapi.dev/api/films/1/",
"http://swapi.dev/api/films/2/",
"http://swapi.dev/api/films/3/",
"http://swapi.dev/api/films/6/"
],
"species": [],
"vehicles": [
"http://swapi.dev/api/vehicles/14/",
"http://swapi.dev/api/vehicles/30/"
],
"starships": [
"http://swapi.dev/api/starships/12/",
"http://swapi.dev/api/starships/22/"
],
"created": "2014-12-09T13:50:51.644000Z",
"edited": "2014-12-20T21:17:56.891000Z",
"url": "http://swapi.dev/api/people/1/"
}
Thanks a lot 😀
Best Answer
A GET request to that URL is returning HTML not just the JSON body.
Produces this:
Update:
Produces this instead: