+ Start a Discussion
sfdc007sfdc007 

[{"message":"Session expired error

Hi,

I need help on the following requirement as follows,

I have a visualforce page where i have a button in it , when i click the button it is calling my REST API apex class where i am passing the opportunity id and it should display

but when i click on the button i am getting the following error as follows

[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]


Let me know what i should do to resove it , i need to display the opportunity record alone on clik of that button through rest api
 
MY VF PAGE :

<apex:page controller="getservice2">
<apex:form >
<apex:commandButton action="{!getResult}" value="REST API" id="theButton"/>
        <br/><br/>        {!myresponse}
</apex:form>
  </apex:page>
MY REST API CLASS :

@RestResource(urlMapping='/opportunity/*')
global with sharing class MyRestResource {

@HttpGet
    global static opportunity doGet() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        //String opportunityId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        opportunity result = [SELECT Id, Name,Account.name,owner.name FROM opportunity WHERE Id = '00628000004KwQ0'];
        return result;
    }

@HttpPost
global static String doPost(String name,
    String phone, String website) {
    opportunity opportunity = new opportunity();
    opportunity.Name = name;
    insert opportunity;
    return opportunity.Id;
}
}
MY APEX CLASS WHERE MY REST API CONTTOLLER IS REFERRED :

public class getservice2
{
    public String myresponse{get;set;}
    public Pagereference getResult()
    {
        HttpRequest req = new HttpRequest();
        Http http = new Http();
        req.setMethod('GET');
        String url = 'https://ap2.salesforce.com/services/apexrest/opportunity';
        req.setEndpoint(url);
        HTTPResponse resp = http.send(req);
        myresponse=resp.getBody();          

        return null;    }
}




Help me pls

Thanks in Advance
Best Answer chosen by sfdc007
KaranrajKaranraj
You have to authenticate in the REST webservice class by setting the session id in the header of your apex rest class.
Try with the below updated code

Controller Class with REST request
public class getservice2
{
    public String myresponse{get;set;}
    public Pagereference getResult()
    {
        HttpRequest req = new HttpRequest();
        req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID()); 
        req.setHeader('Content-Type', 'application/json');
        req.setMethod('GET');
        String url = 'https://ap2.salesforce.com/services/apexrest/opportunity';
        req.setEndpoint(url);
        HTTPResponse resp = http.send(req);
        myresponse=resp.getBody();          
        return null;    
   }
}

Visualforce page
<apex:page controller="getservice2">
<apex:form >
<apex:commandButton action="{!getResult}" value="REST API" id="theButton" rerender="restAPI"/>
        <apex:outputpanel id="restAPI">   {!myresponse} </apex:outputpanel>
</apex:form>
  </apex:page>

 

All Answers

KaranrajKaranraj
You have to authenticate in the REST webservice class by setting the session id in the header of your apex rest class.
Try with the below updated code

Controller Class with REST request
public class getservice2
{
    public String myresponse{get;set;}
    public Pagereference getResult()
    {
        HttpRequest req = new HttpRequest();
        req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID()); 
        req.setHeader('Content-Type', 'application/json');
        req.setMethod('GET');
        String url = 'https://ap2.salesforce.com/services/apexrest/opportunity';
        req.setEndpoint(url);
        HTTPResponse resp = http.send(req);
        myresponse=resp.getBody();          
        return null;    
   }
}

Visualforce page
<apex:page controller="getservice2">
<apex:form >
<apex:commandButton action="{!getResult}" value="REST API" id="theButton" rerender="restAPI"/>
        <apex:outputpanel id="restAPI">   {!myresponse} </apex:outputpanel>
</apex:form>
  </apex:page>

 
This was selected as the best answer
sfdc007sfdc007
thanks dude , it worked