html - Slack incoming webhook: Request header field Content-type is not allowed by Access-Control-Allow-Headers in preflight response -


i try post slack message via fetch api in browser:

fetch('https://hooks.slack.com/services/xxx/xxx/xx', {   method: 'post',   headers: {     'accept': 'application/json, text/plain, */*',     'content-type': 'application/json'   },   body: json.stringify({text: 'hi there'}) })   .then(response => console.log)   .catch(error => console.error); }; 

i following error message:

fetch api cannot load: https://hooks.slack.com/services/xxxxxxx/xxxxx.  request header field content-type not allowed access-control-allow-headers in preflight response. 

what do?

that slack api endpoint unfortunately appears broken in handling of cross-origin requests frontend javascript code—in doesn’t handle cors preflight options request should—so solution seems omit content-type header.

so looks need remove following headers part of request code:

'content-type': 'application/json' 

that 'content-type': 'application/json' part triggers browser cors preflight options request. so, browser allow frontend javascript code send post request you’re trying do, https://hooks.slack.com/services api endpoint must return access-control-allow-headers response header contains content-type in value.

but endpoint doesn’t return header, preflight fails , browser stops right there.

normally when posting frontend javascript api endpoint expects json, adding content-type: application/json header request need , should do. not in case—because particular api endpoint doesn’t handle properly.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -