0

I’m trying to print (system.debug) the response returned from tooling API but the debug is not displaying anything. So, I was wondering if the code below is correct.

HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
req.setHeader('Content-Type', 'application/json');

req.setEndpoint('https://brave-raccoon-5j3npj-dev-ed.lightning.force.com/services/data/v47.0/tooling/sobjects/');
req.setMethod('GET'); 


Http h = new Http();
HttpResponse res = h.send(req);
system.debug(res.getBody());
  • Based on the answers you've gotten so far (and my own attempt to reproduce the issue), it would have been helpful to include the debug log (or a relevant portion of it) in your question. If I didn't already know that you're likely getting a 302 response, I would have probably asked you how you were executing this code (anonymous apex?) and for anything resembling an error message. – Derek F Oct 16 at 18:26
  • Thanks a lot for the advise and the support. – Gerard Bilbatua Oct 16 at 18:46
1

You need to update your code.

HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
req.setHeader('Content-Type', 'application/json');

req.setEndpoint('https://brave-raccoon-5j3npj-dev-ed.lightning.force.com/services/data/v47.0/tooling/sobjects/');
req.setMethod('GET'); 

Http h = new Http();
HttpResponse res = h.send(req);
while (res.getStatusCode() == 302) {
    req.setEndpoint(res.getHeader('Location'));
    res = new Http().send(req);
    system.debug(res.getBody());
}

This stackexchange post has answer to why you need to handle '302' above. [HttpCallout error [StatusCode=302]

Hope this helps.

  • An important note is that the reason why OP got a blank debug in their log is because the body of the response is itself empty when Salesforce gives us this 302 response (and I got a 302 myself when I tried to reproduce OP's issue). – Derek F Oct 16 at 18:21
  • Thanks a lot this was very helpful. This solved the redirection problem but for some reason the debug is still not showing up the objects. – Gerard Bilbatua Oct 16 at 18:25
  • Seems like you are using a scratch org. Could you please paste the debug response. Are you seeing any errors with response? – MandyKool Oct 16 at 18:33

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.