• Siddharth Patni
  • NEWBIE
  • 10 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
String baseURL =  'https://' + System.URL.getSalesforceBaseUrl().getHost();
                String queryStr = 'SELECT+NumLinesCovered,ApexClassOrTriggerId,ApexClassOrTrigger.Name,NumLinesUncovered,Coverage+FROM+ApexCodeCoverageAggregate';
                String ENDPOINT = baseURL + '/services/data/v40.0/tooling/';
               
                HttpRequest req = new HttpRequest();
                req.setEndpoint(ENDPOINT + 'query/?q=' + queryStr);
               
                req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID() );
                req.setHeader('Content-Type', 'application/json');
                req.setMethod('GET');
                req.setTimeout(80000);
                system.debug('end'+req);
            Http http = new Http();
            HTTPResponse res = http.send(req);
           


getting error  [{"message" :"This session is not valid for use with the REST API","errorCode":"INVALID_SESSION_ID"}] while calling tooling api  from LWC


I have also used Connected app and named credential its not working while called from lwc. but, working as expected while I call it using annoynomous apex  

 
String baseURL =  'https://' + System.URL.getSalesforceBaseUrl().getHost();
                String queryStr = 'SELECT+NumLinesCovered,ApexClassOrTriggerId,ApexClassOrTrigger.Name,NumLinesUncovered,Coverage+FROM+ApexCodeCoverageAggregate';
                String ENDPOINT = baseURL + '/services/data/v40.0/tooling/';
               
                HttpRequest req = new HttpRequest();
                req.setEndpoint(ENDPOINT + 'query/?q=' + queryStr);
               
                req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID() );
                req.setHeader('Content-Type', 'application/json');
                req.setMethod('GET');
                req.setTimeout(80000);
                system.debug('end'+req);
            Http http = new Http();
            HTTPResponse res = http.send(req);
           


getting error  [{"message" :"This session is not valid for use with the REST API","errorCode":"INVALID_SESSION_ID"}] while calling tooling api  from LWC


I have also used Connected app and named credential its not working while called from lwc. but, working as expected while I call it using annoynomous apex