Sonar web api request It does not have HTTP ok status

I’m facing the issue Access to fetch at ‘https://XXXXXXXX.xxx.com:9443/api/user_tokens/search’ from origin ‘https://salesforce.lightning.force.com’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: It does not have HTTP ok status.

this is the request i’m sending

import { LightningElement } from 'lwc';

export default class ProjectAnalysis extends LightningElement {
    connectedCallback() {
        this.getProjectAnalysisData();
    }

    getProjectAnalysisData() {
        const token = "XXXXXXXXXXXXXXXXXX";
        const authHeader = "Basic " + btoa(token + ":");

        fetch('https://XXXXXXXX.xxx.com:9443/api/user_tokens/search', {
            method: "GET",
            headers: {
                "Content-Type": "application/json",
                "Authorization": authHeader // Add the Authorization header with the token
            },
        })
        .then((response) => {
            if (!response.ok) {
                throw new Error("HTTP error " + response.status);
            }
            return response.json();
        })
        .then((data) => {
            console.log(data);
        })
        .catch((error) => {
            console.log('Error: ', error);
        });
    }
}

Updated fetch request

import { LightningElement, track } from 'lwc';

export default class FetchData extends LightningElement {
    @track response;

    handleClick() {
        this.fetchData();
    }

    fetchData() {
        fetch("https://salesforcecodescanner.xxx.com:9443/api/authentication/validate", {
            method: "POST",
            headers: {
                "Authorization": "Bearer xxxxxxxxxxxxxxxxxxxx"
            }
        })
        .then((response) => {
            if (!response.ok) {
                throw Error(response.statusText);
            }
            return response.json();
        })
        .then((responseAsJson) => {
            this.response = responseAsJson;
            console.log(this.response);
        })
        .catch((error) => {
            console.error('Looks like there was a problem: \n', error);
        });
    }
}

Still having the same error. from postman the request is successful

Hi,

When you try the request directly via Postman it works, but when you try to integrate it into Salesforce you get a Cross-Origin Resource Sharing error? Sounds like Salesforce is being strict.

When I try your API call against our dogfooding server, I see, among other things, that the response does give a 200 status code.

 
HTH,
Ann