- From: Ivan Artemiev <notifications@github.com>
- Date: Tue, 27 Aug 2019 12:07:54 -0700
- To: whatwg/fetch <fetch@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <whatwg/fetch/issues/924@github.com>
When sending a POST request with `fetch` that contains headers and the `keepalive` option enabled, the request fails, returning the error: "Failed to fetch."
I did not see anything in the spec that would preclude the use of custom headers with `keepalive`.
In the following code, the first `fetch` returns a response, whereas the second one throws the error mentioned above. It exhibits the same behavior with both standard and custom headers.
[Link to the code on CodePen](https://codepen.io/ObjectiveCat/pen/aboWEoa)
```javascript
(async () => {
const URL = 'https://jsonplaceholder.typicode.com/posts';
const body = JSON.stringify({data: 'test'});
// keepalive flag
const fetchOptions = {
method: 'POST',
keepalive: true,
body
}
try {
const result = await fetch(URL, fetchOptions);
console.log(await result.json());
} catch(e) {
console.error(`Fetch failed: ${e}`);
}
// keepalive flag with headers:
const fetchOptionsHeaders = {
...fetchOptions,
headers: {
'Content-Type': 'application/json'
}
};
try {
const resultHeaders = await fetch(URL, fetchOptionsHeaders);
console.log(await resultHeaders.json());
} catch(e) {
console.error(`Fetch with headers failed: ${e}`);
}
})();
```
--
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/fetch/issues/924
Received on Tuesday, 27 August 2019 19:08:16 UTC