function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sunu jsunu j 

Access external api through key

Hi all,
I have access an external api, using api key to retreive jason data from it and parse it later.  I am trying to access the api, through this code.  this doesn;t return me any data.  But curl on that api from another linux box returns me data, where i am i wrong here, please help me to understand this better as i am new to api concept.  The code i am using is

public class testCallouts{
// Pass in the endpoint to be used using the string url
  public String getContent(String url) {
// Instantiate a new http object
    Http h = new Http();
// Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
    HttpRequest req = new HttpRequest();
    req.setEndpoint(url);
    req.setMethod('GET');
// Send the request, and return a response
    HttpResponse res = h.send(req);
   return res.getBody();
   //return res.toString();
  }
testCallouts testCall = new testCallouts();
String ret=testCall.getContent('https://sandbox.example.com/api/v2/accounts?key=xxxxxxxxxxxx');
system.debug(ret);
}
ret returns null
sunu jsunu j
To help someone who might face this issue later, i am posting how i solved it

Added these lines, as i was getting a status code 406

req.setHeader("Accept", "application/json");
req.setHeader('Content-Type', 'application/json');
AmitAmit (Salesforce Developers) 
Hello,

Please refer below links for more information:

http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_methods_system_json.htm https://developer.salesforce.com/page/Getting_Started_with_Apex_JSON  http://www.salesforce.com/us/developer/docs/api_rest/api_rest.pdf https://developer.salesforce.com/page/Getting_Started_with_the_Force.com_REST_API https://developer.salesforce.com/page/Digging_Deeper_into_OAuth_2.0_on_Force.com
sunu jsunu j
Thanks Amit, i got it fixed by putting the header