We are using HTTP callout within Salesforce logged in session but when i run the below code it is giving the below error as a response
System.HttpResponse[Status=Unauthorized, StatusCode=401]
[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]
@future(callout=true)
public static void doTerritoryCalloutAsync() {
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v30.0/sobjects/UserTerritory');
req.setBody('{ "TerritoryId" : record , "UserId" : userId}');
req.setHeader('Authorization','Bearer '+UserInfo.getSessionId());
req.setHeader('Content-Type', 'application/json');
req.setMethod('GET');
try{
system.debug('***Request Method Wala***'+req);
HttpResponse res = h.send(req);
if(res.getStatusCode() != 200){
system.debug('user assigned to territory:'+ res.getBody());
}
else{
system.debug('user assigned to territory:'+ res.getBody());
}
}
catch(Exception e){
system.debug('e.getMessage()');
}
}