+ Start a Discussion
Jim WarnerJim Warner 

REST/JSON API Callout seems to return html

This question refers to the following:
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_json_jsonparser.htm

Following the example in the link above, I've added http://www.cheenath.com as a remote site.  When I execute the following snippit of the example code in the Execute Anonomous Window of the developer console:

        Http httpProtocol = new Http();
        // Create HTTP request to send.
        HttpRequest request = new HttpRequest();
        // Set the endpoint URL.
        String endpoint = 'http://www.cheenath.com/tutorial/sfdc/sample1/response.php';
        request.setEndPoint(endpoint);
        // Set the HTTP verb to GET.
        request.setMethod('GET');
        // Send the HTTP request and get the response.
        // The response is in JSON format.
        HttpResponse response = httpProtocol.send(request);
        System.debug(response.getBody());

I get back what looks like html in the return value (in the log output) from the resspone.getBody() method call.  

When I execute the full code in the parseJSONResponse method, I get the following error:

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

So it looks like the JSONParser is also seeing html.

What's up with this?  I get this same error on another 3rd Party site where I'm making a REST API Callout and expecting JSON as the return value.  Seems like I must be missing something fundamental in how I'm making the REST calls.  Am I looking at the wrong part of the response?  

I have also tried setting the following in the request header:
req.setHeader('content-type', 'application/json');
It doesn't make any difference in what I get back in the body.

On the other 3rd Party website, when I execute some test PHP code that makes the same REST call, I do get JSON data back, so the API seems to be working.

Thanks for any help you can give! 
pconpcon
I think the problem seems to be with your endpoint.  If I navigate to it via the browser I get taken to a page that 404s [1].  If I run a cURL against the resource I get:

<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>nginx</center>
</body>
</html>

which is not the data you would expect.

[1] https://twitter.com/tutorial/sfdc/sample1/response.php/cheenath
pconpcon
If I add date.jsontest.com to my sites and run the following, I get the expected data back

Http httpProtocol = new Http();
HttpRequest request = new HttpRequest();
String endpoint = 'http://date.jsontest.com/';
request.setEndPoint(endpoint);
request.setMethod('GET');
HttpResponse response = httpProtocol.send(request);
System.debug(response.getBody());

What other endpoints are you testing against?
Jim WarnerJim Warner
That endpoint does seem to return some JSON data back, so that is good.  The other site I'm working with is: http://betasandbox.api.chapterspot.info.  The API is search/members.  The complete endpoint string is:

http://betasandbox.api.chapterspot.info/search/members?client_userid=12345&client_id=triangle_connector&client_secret=b898f4086312ee94fc98982906b406e1fd

It seems to work fine in a browser, but not from Apex.

pconpcon
This is odd, I would contact the owner of the api and discuss it with them.  The error I am seeing with that endpoint appears to be a server side error:

<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
     <h4>A PHP Error was encountered</h4>
     <p>Severity: Notice</p>
     <p>Message:  Array to string conversion</p>
     <p>Filename: libraries/Table.php</p>
     <p>Line Number: 353</p>
</div>
<table border="0" cellpadding="4" cellspacing="0">
     <thead>
          <tr>
               <th>status</th>
               <th>data</th>
          </tr>
     </thead>
     <tbody>
          <tr>
               <td status='success'>Array</td>
          </tr>
     </tbody>
</table>
pconpcon
Also, it's probably a good idea to delete your app and re-create it since you have just shared the the client_userid, client_id and client_secret for the request
Jim WarnerJim Warner
Yeah, good point, though it is only a sandbox api.  I wonder why it seems to work fine from a browser and when I call it via PHP?   No error is returned in either of those scenarios.  I will definitely contact them Monday.  Thanks for your help.
BijaySBijayS
Hi ,

Even i am facing the issue.

Unexpected character ('E' (code 69)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at input location [1,2]

I am calling a HTTP Get method in one of the Salesforce Org (Say SF2) from one more salesforce org (Say SF1)

Basically i am doing a future callout from SF1 from trigger,after insert ....if the Account Exist in SF2 then update the Acount references in SF1.

Here i am passing Single Account Id just for example...passing the Account name in the End Point URL and in SF2 querying the same and returning based on match found.

While Creating Account If i Give Account Name without Space e.g Test Account ,it gives below error :

Unexpected character ('E' (code 69)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at input location [1,2]

But if i give TestAccount it passes and returns the result,may be its not taking the white spaces.


Just wanted to know how can i remove the space and again retrieve the same after sending back to external system to find the match.
May be i am missing something.

Thanks in Advance.