I am trying to read a response from a Cloudflare Worker API, I have not set anything sepcial about it and in Postman the request works as expected. I am guessing I am missing some header but I am not sure which one or even if that is the reason reading the reponses body is not working in browser.
This is how I fetch the data:
JavaScript
x
22
22
1
const url = `${process.env.SOME_URL}/v1/method?token=${TOKEN}`
2
const headers = new Headers();
3
headers.append("Content-Type", "application/json;charset=UTF-8");
4
5
6
const requestOptions = {
7
method: 'POST',
8
redirect: 'follow',
9
mode: 'no-cors',
10
body: JSON.stringify(body),
11
headers
12
// referrerPolicy: 'no-referrer',
13
};
14
15
return fetch(url, requestOptions)
16
.then(async (response) => {
17
// no body to be parsed
18
return response.json()
19
} )
20
.then(result => console.log(result))
21
.catch(error => console.log('error', error));
22
Looking forward to your answers, thank you.
Advertisement
Answer
This behaviour is due to the no-cors
config of the fetch-api
.
For more details read about opaque requests here: