I keep getting 401 Not Authorized errors when I call my resource with a REST test tool like the Advanced REST client. I'm supplying an access token taken from a curl call to the salesforce login service. The error I get back when using the RestResource is

  message: "Session expired or invalid"

My annotation for the RestResource is

@RestResource( urlMapping='/CreateObject/v1/*')

And I'm calling my service via the URL https://emea.salesforce.com/services/apexrest/CreateObject/v1/*

Can anyone shed a light as to what I'm doing wrong?

  • do not add the * at the end of the REST url when you're making the request from client..
    – Sam
    Jul 17, 2013 at 11:45

2 Answers 2


You should also include the session ID in your rest call

for eg if you're calling from jQuery Ajax include the authorization header like below:

url : "https://instance/services/data/v26.0/sobjects/global/describe/layouts/",
headers : {"Authorization": "Bearer AddyoursessionIdhere"},
contentType : "application/json",
type :"GET"
    console.log('the result '+ JSON.stringify(response));

    console.log('the error mesasge is '+ JSON.stringify(error));

or in curl, include the below header along with the request

-H "Authorization: Bearer sessionId

You can go thru (authentication section) this on how to get the session Id in curl.. http://www.salesforce.com/us/developer/docs/api_rest/

But if you want to make the REST public then you can look it up here: http://www.wadewegner.com/2013/03/creating-anonymous-rest-apis-with-salesforce-com/

Also instead of using the REST client you can use this link http://www.hurl.it to make simple GET requests

  • I'm using hurl.it to do this, but I think my problem stems from the fact that my session Id is somehow wrong, or invalid. Since I'm getting a 401 Not Authorized.
    – akarnid
    Jul 17, 2013 at 12:31
  • okay, if you're logged into salesforce in your browser you can get the session id from the cookie, the name of the cookie is 'sid'. In Chrome, you can find the cookie by going to settings->advanced settings->privacy->content settings->allcookie and site data and then seacrch for yourinstance.salesforce.com and select the sid cookie and the copy the content of this cookie. When using the same in hurl make sure you add the two headers one for "authorization": "Bearer sessionId" and the other for "content-Type": "application/json"
    – Sam
    Jul 17, 2013 at 12:37

I'm pasting my code here for reference, this will surely help

var result = sforce.connection.login("[email protected]", "mypassword+mysecurityToken");
sforce.connection.init('{!sessionId}', 'REST web service url'); // here pass current session id of the org from which you are making request.

       url : REST web service url,
       requestHeaders: {"Authorization":"Bearer "+result.sessionId, "Content-Type":"application/json"}, // here pass the session id of the org in which you have your REST service
       requestData: data to post in JSON format,
       method: "POST",
       onSuccess : function(response) {
       onFailure : function(response) {
              alert("Failed" + response)

If any difficulty, please comment on this post.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .