0

I try to create a Header for a following fetch() like this

var myheaders = new Headers(
   { "Accept": "application/json",
     "Content-Type": "application/json; charset=UTF-8"
   });

let b = JSON.stringify ({ "cmd2" : "ytdl", "url" : "x"});

let params = 
{ headers : myheaders,
  body : b,
  method : "POST",
  mode : "no-cors"
};
let response = await fetch("http://127.0.0.1:5000/ytdl",params);
....

If I print the headers in the receiving Server (Flask) I get:

Host: 127.0.0.1:5000
Connection: keep-alive
Content-Length: 67
Accept: application/json
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6)        AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36
Sec-Fetch-Mode: no-cors
Content-Type: text/plain;charset=UTF-8
Origin: chrome-extension://mnihgjnpmkpgeichhdfhejagbefjpnnb
Sec-Fetch-Site: cross-site
Accept-Encoding: gzip, deflate, br
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7

Any Idea, what I´m doing wrong?

davidism
  • 121,510
  • 29
  • 395
  • 339
mica
  • 3,898
  • 4
  • 34
  • 62

1 Answers1

1

I didn't understand what is the reason but when you call without mode: 'no-cors' content type is:

let params = {
  headers : myheaders,
  body : b,
  method : "POST",
  mode : "cors"
};

response = await fetch("http://127.0.0.1:5000/", params);

The output of the flask request.headers:

...
Accept: application/json
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36
Sec-Fetch-Mode: cors
Content-Type: application/json; charset=UTF-8
...
metmirr
  • 4,234
  • 2
  • 21
  • 34
  • I set the mode to 'no-cors', because I got the error "Access to fetch at 'http://127.0.0.1:5000/ytdl' from origin 'chrome-extension://mnihgjnpmkpgeichhdfhejagbefjpnnb' has been blocked by CORS policy:" With adding the source to the permissions in the manifest it also works with mode "cors" – mica Aug 18 '19 at 19:36
  • I´m interesten in the question, why the contnettype isn't transferred in"no-cors" mode – mica Aug 18 '19 at 19:37