Reputation: 25
I'm trying to execute query using REST API, in a lightning web component.
The request in Postman returning result with success (enabling Follow Authorization header), but in the JavaScript in lightning web component it returns 401 Unauthorized.
The code in the Javascript is a follows:
let sessionId = 'token';
let baseUrl = window.location.origin;
let header = {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": "Bearer " + sessionId,
};
if (sessionId) {
let options = {
method: "GET",
mode: 'no-cors',
redirect: 'follow',
headers: header,
};
fetch(baseUrl + '/services/data/v50.0/query/?q=SELECT+name+from+Account', options).then((response) => {
console.log(JSON.stringify(response));
if (!response.ok) {
// throw Error(JSON.stringify(response));
} else {
return response.json();
}
}).then((repos) => {
console.log(repos, repos);
});
}
Am I missing something?
Upvotes: 1
Views: 1793
Reputation: 2129
Since you can not pass the value Authorization
to no-cors
mode, you will need to add CORS configuration in your SalesForce as safe endpoint where they let you make a call.
Upvotes: 1
Reputation: 1
You can not send Authorization header with "no-cors" mode.
mode: "no-cors"
only allows a limited set of headers in the request:
Accept
Accept-Language
Content-Language
Content-Type with a value of application/x-www-form-urlencoded, multipart/form-data, or text/plain
Upvotes: 0