'POST getting 415 (Unsupported Media Type) while POSTMAN returns 200

Seems like a simple issue but everywhere else suggested adding "application/json" in 'headers' which I have tried.

'Accept'and 'Content-Type' are both "application/json". Also tried both json and stringfy 'Body' but keep getting 415 on Chrome extension JavaScript.

textArraySample = ["sample","sample2"];
var serverUrl = "https://webappcontentnotification.azurewebsites.net/api/ContentUpload";
const body={
    "textbatch":textArraySample,
    "userId": userId,
    "url": window.location.href
}
let result = 
fetch(serverUrl, {
    method: 'POST',
    mode: "no-cors",
    headers: {
        'Accept': "application/json",
        'Content-Type': "application/json"
    },
    body: JSON.stringify(body)
    })
.then(response => {
    console.log('response:', response);
})
.catch((error) => {
    console.log('Error:', error);
});

Update: seems like "Content-Type" is not correctly set enter image description here



Solution 1:[1]

Did not see this mentioned somewhere else. The root cause is that "Content-Type" cannot be set to "application/json" while using "Mode: no-cors". link here

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daolin