0

I am getting this error when trying to deserialize an HTTP response - hope you can help:

FATAL_ERROR System.JSONException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at input location [2,2]

Very quickly, I am doing a POC on connecting two Salesforce orgs via REST. I have created a super simple REST service in a sandbox and am now trying to call it from a different sandbox. I am able to connect, but how do I deserialize the response get - the access token - and call my REST service?

This is my code - I just call it through execute anonymous for now: String accessToken; String sfdcInstanceURL;

try{
    HttpRequest req = new HttpRequest();
    req.setEndpoint(ENP_POINT_URL);
    req.setMethod('POST');
    Blob headerValue = Blob.valueOf(USERNAME + ':' + PASSWORD);
    String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
    req.setHeader('Authorization', authorizationHeader);
    req.setHeader('Accept','application/json');
    req.setBody(String.format(REQUEST_BODY ,new string[]{CONSUMER_KEY,CONSUMER_SECRET,
                                                         USERNAME,PASSWORD}));
    req.setTimeout(60000);
    Http http = new Http();
    HttpResponse hRes = http.send(req);

    Map<String, String> results = (Map<String, String>) JSON.deserializeUntyped(hRes.getBody());
    //Map<String,String> res = (Map<String,String>) JSON.deserialize(hRes.getBody(),Map<String,String>.class);

    accessToken = results.get('oauth_token');
    sfdcInstanceURL = results.get('loginURL');

    System.debug('Response1: ' + hres.getBody());
    System.debug('Response1: ' + hres);

    System.debug('accessToken: ' + accessToken);
    System.debug('sfdcInstanceURL: ' + sfdcInstanceURL);

    req = new HttpRequest();
    req.setEndpoint(ENP_POINT_URL2);
    req.setMethod('POST');
    req.setHeader('Content-Type', 'application/json;charset=UTF-8');
    req.setBody('{"name":"mighty moose"}');

    //System.debug('Response3: ' + res.getBody());
    //System.debug('Response3: ' + res);     

    //OAuth objAuthenticationInfo = (OAuth)JSON.deserialize(res.getbody(), OAuth.class);
    //return objAuthenticationInfo;
}catch(CallOutException ce){
    throw ce;
}

This is the response body: enter image description here

enter image description here

2
  • 1
    Please paste the response body as text in your question. Based on your screenshot, it appears that it is not JSON.
    – David Reed
    Commented Feb 11, 2019 at 14:54
  • Hi David, can you direct me how to do that. If I remove the line that returns the error and I System.debug the responce.getBody(), the above is what I get.
    – MR.M.
    Commented Feb 12, 2019 at 7:30

1 Answer 1

3

This error typically happens when the call you are making returns an error page that is HTML rather than JSON. System.debug the string before trying to deserialize to see what is going on.

8
  • Hi Keith, I have pasted the response in the question above - can you help me debug this? I have no clue on how to proceed.
    – MR.M.
    Commented Feb 11, 2019 at 14:41
  • @user2936057 That looks like a login page, so you have not successfully authenticated before making the request.
    – Keith C
    Commented Feb 11, 2019 at 14:54
  • Hi Keith, this is the authentication request... So you are saying that the authentication didn't work. Why do I get status OK - status code 200 then? Can you help me debug and understand the issue?
    – MR.M.
    Commented Feb 12, 2019 at 7:33
  • @user2936057 There are a number of choices on how to authenticate and holding/sending the username/password is probably the weakest approach so not something to invest a lot of time in. I don't know if the approach you are using can be made to work; appending the security token to the password might help. But I suggest you choose the most appropriate one of these Digging Deeper into OAuth 2.0 in Salesforce approaches instead as they are well documented (though a little puzzling at first).
    – Keith C
    Commented Feb 12, 2019 at 8:04
  • Hi Keith, I will try to connect with Named Credentials instead - although I am bit reluctant to leave my approach without it working, since I really just wanted to use this for some testing. You do not have any ideas on what could be wrong on the example above? Thx.
    – MR.M.
    Commented Feb 12, 2019 at 11:01

You must log in to answer this question.

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