2

I want to make rest api callout (to same salesforce org) without apex i.e. from LWC using Javascript (fetch, xhr). I have created a Named credential for making callout. And I am trying to upload file from LWC where the user is not logged in. I cant use apex because of heap size issue.

So Is there a way to call the rest api end point from LWC JS without any user credentials or tokens, like we make apex http callout to rest end point using named credential ?

0

So Is there a way to call the rest api end point from LWC JS without any user credentials or tokens, like we make apex http callout to rest end point using named credential ?

NO

In apex, you would make a named credential callout as shown in the code snippet below:

HttpRequest req = new HttpRequest();
req.setEndpoint('callout:My_Named_Credential/some_path');

This means that the setEndpoint method of HttpRequest object takes the responsibility of identifying the named credential and passing the auth tokens along with the request.

Fetch & XmlHttpRequest are Javascript based WebAPIs and are not maintained by Salesforce. This means that when you use them, you will need to handle/ pass the auth parameters yourself. Besides, if you are making an client side web request using any of these APIs, your code will not be hitting Salesforce servers (which maintains the named credentials) at all.

Note: You could write a generic LWC utility which would invoke the apex code and create a web request, but that wouldn't require you to use fetch or xhr.

0

For doing callouts to Salesforce specifically, you should have token.

Now you can follow below ways to have token stored/passed to JS(with low/no apex ways) -

  1. Passing from Apex - So simple string can be easily passed. Trailhead link
  2. You can have it passed from VF page as a parameter to LWC component - E.g.
<c:component-name sessionId ='{! sessionId }' />
  1. You can have fetch session Id from cookies - sid, is nothing but token(sessionId).
  2. You can store it on browser with encrypted from caller component(with the help of storage APIs of browser) - Not recommended.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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