6

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

4
  • 1
    It appears you are getting a different response body, probably HTML or XML. Can you System.debug() the return value to start figuring out what the issue is? (You can edit to add information).
    – David Reed
    May 28, 2020 at 23:55
  • Hi David. The thing is that I'm executing this from an Anonymous Window so I'm getting this error straight after, and it won't let me debug anything, I've tried to debug but can't. I'm sure the response is a JSON because I've tried calling the endpoint from Postman and returned a JSON, so I don't see what it can be. May 29, 2020 at 0:11
  • 1
    You can get a debug log from Execute Anonymous. It'll be available in the Logs tab of your Developer Console.
    – David Reed
    May 29, 2020 at 0:17
  • Solved, but thank you very much for your time David ! May 29, 2020 at 0:39

2 Answers 2

11

A GET request to that URL is returning HTML not just the JSON body.

HttpRequest req = new HttpRequest();
req.setHeader('Content-Type','application/json;charset=UTF-8');
req.setEndpoint('https://swapi.dev/api/people/1/');
req.setMethod('GET');
HTTPResponse res = new Http().send(req);

system.debug(res.getBody());

Produces this: enter image description here

Update:

HttpRequest req = new HttpRequest();

req.setHeader('Accept', 'application/json'); // NEW AND IMPROVED, SEND US JSON!

req.setHeader('Content-Type','application/json;charset=UTF-8');
req.setEndpoint('https://swapi.dev/api/people/1/');
req.setMethod('GET');
HTTPResponse res = new Http().send(req);

system.debug(res.getBody());

Produces this instead: enter image description here

0
0

If you are using asynchttpclient, you can use this.

Response response = this.asyncHttpClient.prepareGet(dBoxSyncConfig.getUrl())
                .setBody(requestBody)
                .addHeader(HttpHeaders.ACCEPT, "application/json")
                .addHeader(HttpHeaders.AUTHORIZATION, "Basic XYZ")
                .addHeader(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8")
                .execute()
                .get();
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .