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
Biswajit Nath 9Biswajit Nath 9 

HTTPResponse is returning no values

Hi,
I am trying to use ToolingAPI to fetch Pagelayout informations. As I am using lightning, so I have created
1.connected app:
Connected App
2.Auth Provider:
Auth Provider3.Named Credential:
Named CredentialPlease find below the the code snippet:
Global with sharing class DocGeneration {
    @AuraEnabled 
    public static String Callout(){;
        String resbody;
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('callout:Tooling_API/services/data/v35.0/tooling/query/?q=Select+Id+from+Layout');  
        req.setMethod('GET');
        req.setHeader('Content-Type', 'application/json;charset=UTF-8');
        system.debug('req:' + req);
        HttpResponse res = new HttpResponse();
        res = http.send(req);
        resbody= res.getBody();
        system.debug('res:' + resbody);
        return resbody; 
    }
}
When I am calling this method from annonymous Apex in developer console, It is returning the status as 200 but HTTPResponse has no body.
User-added image
Am I doing anything wrong? Please help.
Best Answer chosen by Biswajit Nath 9
ayu sharma devayu sharma dev
Hello Biswajit

I made some changes in your code. As I have ran the code in classic so I used my org's Instance url and ans Session Id as below and I got response in the assert.
 
String resbody;
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm()+'/services/data/v35.0/tooling/query/?q=Select+Id+from+Layout');  
req.setMethod('GET');
req.setHeader( 'Authorization', 'Bearer '+userInfo.getSessionId() );
req.setHeader('Content-Type', 'application/json;charset=UTF-8');
system.debug('req:' + req);
HttpResponse res = new HttpResponse();
res = http.send(req);
resbody= res.getBody();
system.assert(false, 'res:' + resbody);

 You said you are also getting the code 200 which means your authentication is successfully performing. I would suggest you to assert the response and execute the code in execute annonymous. 

Please try and let me know the results.

Regards
Ayush

All Answers

ayu sharma devayu sharma dev
Hello Biswajit

I made some changes in your code. As I have ran the code in classic so I used my org's Instance url and ans Session Id as below and I got response in the assert.
 
String resbody;
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm()+'/services/data/v35.0/tooling/query/?q=Select+Id+from+Layout');  
req.setMethod('GET');
req.setHeader( 'Authorization', 'Bearer '+userInfo.getSessionId() );
req.setHeader('Content-Type', 'application/json;charset=UTF-8');
system.debug('req:' + req);
HttpResponse res = new HttpResponse();
res = http.send(req);
resbody= res.getBody();
system.assert(false, 'res:' + resbody);

 You said you are also getting the code 200 which means your authentication is successfully performing. I would suggest you to assert the response and execute the code in execute annonymous. 

Please try and let me know the results.

Regards
Ayush
This was selected as the best answer
Biswajit Nath 9Biswajit Nath 9
Hi ayu,
I can see the result but I am unable to store it in a string. Is there any way? Thanks in advance