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
Adam Drissel 1Adam Drissel 1 

Getting REST API error message that isn't found anywhere

When I make a call to the API (code below) I get the following error message returned:
System.HttpResponse[Status=Unknown Version, StatusCode=400]

Here is the code I am using:
public static void callRestApi(){
        String instance = getSFDCInstance();
        String sessionId = getSessionId();
        String url = instance+'/services/data/v37.0/sobjects/Case/describe/ -H \"Authorization: Bearer'+sessionId+'\"';
        
        system.debug(url);
        
        Http h = new Http();
        
        HttpRequest req = new HttpRequest();        
        req.setEndpoint(url);
        req.setMethod('POST');
		req.setHeader('Charset', 'UTF-8');
        req.setHeader('Content-Type', 'text/plain');
        
        system.debug(req);
        
        HttpResponse res = h.send(req);
        String body = res.getBody();
        
        system.debug(body);
    }

​I've tried a "GET" call as well and get the same exact response.  What "version" is unknown?  My URL is formatted exactly as the Salesforce REST API documentation has it.

Please help ASAP.
Adam Drissel 1Adam Drissel 1
Oh, and I should also note that I'm trying to get an API Session Id inside a Lightning Component.  How can I do this?
Raj VakatiRaj Vakati
Hi Adam , 
Code is for you . Use this one 

public class callRestApi {
    public static void callRestApi1(){
        String sessionId =UserInfo.getSessionId();
        
        Http h = new Http();
        //curl https://NA50.salesforce.com/services/data/v30.0/sobjects/Account/describe/approvalLayouts/ -H "Authorization: Bearer token"
        
        HttpRequest req = new HttpRequest();        
        // req.setEndpoint('https://'+'NA50.salesforce.com'+'/services/data/v37.0/sobjects/case/describe -H \"Authorization: Bearer '+sessionId+'\"');
        req.setMethod('GET');
        
        String val ='https://fscttt-dev-ed.my.salesforce.com/services/data/v37.0/sobjects/case/describe' ;
        System.debug('===================>'+val);
        req.setEndpoint(val);
       
        req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
        
        
        HttpResponse res = h.send(req);
        System.debug('res Status'+res.getStatus());
        String body = res.getBody();
        
        system.debug('=='+body);
    }
}
 
Raj VakatiRaj Vakati
Hi Adam , 

​Please follow some best practices and patterns . this code was for testing
 
 
 
Adam Drissel 1Adam Drissel 1
Thanks Rajamohan!  

I am trying to make the API call from within a Lightning App.  I've been reading up a little more on this and it seems to explain why I continue to get the "Session Invalid" error status even if everything is correct.  According to the Lightning Dev Manual (https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/js_api_calls_platform.htm) I can invoke it from my Apex Controller, as long as I used Named Credentials.  But, I have tried that and get the same error there.  

Any wisdom on that?