+ Start a Discussion
Nithesh NNithesh N 

Rest Callout From Client Side Controller ?

Lets Say I want to Create a Lightning Component in Which I need to a Rest Callout. 
My Question is , Instead of Doing it through Server-Side Controller, Can I Just do the Call from Clientside and call the server-side controller later according to the results of callout?

Please share your thoughts on this and let me know if I am missing something here...

Thanks in Advance.
Best Answer chosen by Nithesh N
sfdcMonkey.comsfdcMonkey.com
hi Nithesh 
  By default, you can’t make calls to third-party APIs from client-side code. Add a remote site as a CSP Trusted Site to allow client-side component code to load assets from and make API requests to that site’s domain.

              The Lightning Component framework uses Content Security Policy (CSP) to control the source of content that can be loaded on a page. Lightning apps are served from a different domain than Salesforce APIs, and the default CSP policy doesn’t allow API calls from JavaScript code. You change the policy, and the content of the CSP header, by adding CSP Trusted Sites.
       You can’t load JavaScript resources from a third-party site, even a CSP Trusted Site. To use a JavaScript library from a third-party site, add it to a static resource, and then add the static resource to your component. After the library is loaded from the static resource, you can use it as normal.
Sometimes, you have to make API calls from server-side controllers rather than client-side code. In particular, you can’t make calls to Salesforce APIs from client-side Lightning component code. For information about making API calls from server-side controllers.
Source :Lightning Components Developer Guide
thanks 
Let me know if it helps you and mark it as solved if it helps you so it make proper solution for others in future 

All Answers

sfdcMonkey.comsfdcMonkey.com
hi Nithesh 
  By default, you can’t make calls to third-party APIs from client-side code. Add a remote site as a CSP Trusted Site to allow client-side component code to load assets from and make API requests to that site’s domain.

              The Lightning Component framework uses Content Security Policy (CSP) to control the source of content that can be loaded on a page. Lightning apps are served from a different domain than Salesforce APIs, and the default CSP policy doesn’t allow API calls from JavaScript code. You change the policy, and the content of the CSP header, by adding CSP Trusted Sites.
       You can’t load JavaScript resources from a third-party site, even a CSP Trusted Site. To use a JavaScript library from a third-party site, add it to a static resource, and then add the static resource to your component. After the library is loaded from the static resource, you can use it as normal.
Sometimes, you have to make API calls from server-side controllers rather than client-side code. In particular, you can’t make calls to Salesforce APIs from client-side Lightning component code. For information about making API calls from server-side controllers.
Source :Lightning Components Developer Guide
thanks 
Let me know if it helps you and mark it as solved if it helps you so it make proper solution for others in future 
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Yes you can do the same fron java script. Please check below post
1) http://amitsalesforce.blogspot.com/2017/04/http-callout-from-java-script-rest.html

Let us know if this will help you