0

I am implementing the lightning component and APEX controller to make the call to REST API .I am able to get the response from the REST client but if i call it through APEX controller it is saying 404 status code. I also added my URL in remote settings, and tried with google books URL also

the sample URL i tried are

http://bin.org/get

http://api.fixer.io/latest?base=USD

https://www.googleapis.com/books/v1/volumes/NFPqCQAAQBAJ

What are the posibilities for not getting the response..

Here is my code please check it.

public  class WebservicesController {
    public static String responseFormat='application/json';
    public static String bodyContentType='application/json';

       @AuraEnabled
       public static Response makeRequest(String url,  String method, String 
       bodyContent) {
        System.debug('Making request httpResponse ' );
         system.debug(url.getSalesforceBaseUrl().toExternalForm());

       HttpRequest request = buildRequest(url, method,bodyContent);
       HttpResponse httpRes = sendRequest(request);
      Response restRes = buildResponse(httpRes);
       return restRes;

}

    private static HttpRequest buildRequest(String url, String method, 
      String bodyContent) {
   HttpRequest request = new HttpRequest();
   System.debug('Making request httpResponse '+ url );
   request.setEndpoint(url);
   request.setMethod(method);
   request.setHeader('Content-Security-Policy', '*');

   if (bodyContent != null) {
       request.setBody(bodyContent);
       request.setHeader('Content-Type', bodyContentType);
   }

   request.setHeader('ACCEPT', responseFormat);

   return request;

}

    private static HttpResponse sendRequest(HttpRequest request) {    
      return new Http().send(request);
     }

   private static Response buildResponse(HttpResponse httpRes) {
   Response restRes = new Response();

   restRes.status = httpRes.getStatus();
   restRes.statusCode = httpRes.getStatusCode();
   restRes.body = httpRes.getBody();
           System.debug(' Status code is  ' + restRes.statusCode  );

   System.debug(' httpResponse ' + httpRes.getBody() );

   return restRes;

}

   public class Response {

   @AuraEnabled
   public String status { get; set; }

   @AuraEnabled
   public Integer statusCode { get; set; }

   @AuraEnabled
   public String body { get; set; }

} }

  • Would you post your apex code? bin.org/get is down and giving 404, so dont use it. – Pranay Jaiswal Apr 4 '18 at 8:09
  • @Pranay Jaiswal I posted my code please check – Roster Apr 4 '18 at 9:05
  • what is the method you are trying? is it get or post? if it is get, its not needed to send the body here – Preya Apr 4 '18 at 9:18
  • Yes it is GET If i remove that param also Same problem. – Roster Apr 4 '18 at 9:20
1

Execute the following code in anonymous window of developer console

system.debug('my url = '+URL.getSalesforceBaseUrl().toExternalForm());

you will get your instance url which will be slightly different than home page URL.

Add this URL in remote site setting & check.

| improve this answer | |
  • My developer console is throwing this eror . "Method does not exist or incorrect signature: void getSalesforceBaseUrl() from the type String" – Roster Apr 4 '18 at 8:56
  • Issue fixed or getting above error? – Shilpa Apr 4 '18 at 9:25
  • Not fixed. Getting same error.. – Roster Apr 4 '18 at 9:25
  • @Shilpa shes is not doing callout to SF instacne url, how will adding it in remote site settings help? – Pranay Jaiswal Apr 4 '18 at 9:35
  • Yes Pranay you are correct, the SF instance URL is needed in remote site setting only if we are hitting API call in the same org. My apologies. – Shilpa Apr 4 '18 at 10:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.