You need to sign in to do that
Don't have an account?

When token is missing or expired CORS error returned instead of 401
Hi,
I am developping a Salesforce integration in a web aplication. I can login and request data with the received token. I try to catch the 401 response when the token expires, however I get a CORS error when I don't include the Authorization header or pass in an expired token. Could anyone help me how to solve this? I need to implement a token refresh strategy by listening to a 401 response.
I am developping a Salesforce integration in a web aplication. I can login and request data with the received token. I try to catch the 401 response when the token expires, however I get a CORS error when I don't include the Authorization header or pass in an expired token. Could anyone help me how to solve this? I need to implement a token refresh strategy by listening to a 401 response.
Have you whitelisted origin URL in CORS allowlist
https://developer.salesforce.com/docs/atlas.en-us.chatterapi.meta/chatterapi/extend_code_cors.htm
Thanks,
The origin is whitelistes. I can request everything with a valid token. There are no cors issues then. This happens specifically with an expired token or empty authorization header. Instead of the expected 401 I get a cors error response. It looks like the absence of a token or an invalid token is not handled correctly for cors requests.
Thanks,
There are no issues with Postman. It is a specific browser issue. Somehow the Access-Control-Allow-Origin is only sent back if a request contains a valid access token. However, this prevents me from checking when an access token is expired and the refresh token should be used to request a new access token.
Thanks,
Please note that you have to run it from http://localhost or http://localhost:8080, as these two are whitelisted in the cors settings for this sandbox environment.
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
myHeaders.append("Authorization", "Bearer null");
myHeaders.append("Cookie", "BrowserId=1-AzrmCKEeuqlK38s4sJwA");
var requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
};
fetch("https://bridge2-dev-ed.my.salesforce.com/services/data/v50.0/sobjects", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
If you need a more complete example, I can give you a github repo to clone later.