3

I am trying to make a cors PUT request using the vanilla Fetch API

When I click my button to send the PUT request, the method on the first request is OPTIONS. It is only when I click the button again, then the method on the request changes to PUT. Why?

I understand this is part of the CORS preflight, but is there a way to trigger the preflight manually so the OPTIONS response can be cached?

Could this behavior be indicative of a failing promise somewhere?

6
  • Have you read the specification? Commented Feb 18, 2017 at 4:35
  • Yes, I've read the spec. I'm wondering how this is handled -- do I need to check the already sent request to see if it's an OPTIONS request then send it again? Commented Feb 19, 2017 at 23:28
  • Is the specification not clear? Commented Feb 19, 2017 at 23:29
  • No, it's not. Does the preflight fetch happen manually or automatically? Commented Feb 19, 2017 at 23:46
  • Can you include the javascript that you are trying at Question? You could perform the request manually, if, from what can gather, the preflight request is occurring automatically. fetch("url", {method:"OPTIONS"}) .then(response => response.headers) .then(_headers => Array.from(_headers.entries(), h => console.log(h))).catch(err => console.error(err)), else, from reading of specification, a network error would occur. If from your interpretation the specification is not clear, would suggest searching for a similar issue, or filing an issue at github.com/whatwg/fetch/issues Commented Feb 20, 2017 at 0:32

1 Answer 1

3

See the Fetch Standard, section 4.7. CORS-preflight fetch.

Note: This is effectively the user agent implementation of the check to see if the CORS protocol is understood. The so-called CORS-preflight request. If successful it populates the CORS-preflight cache to minimize the number of these fetches.

at steps 1 through 7; also 4.8. CORS-preflight cache.

Sign up to request clarification or add additional context in comments.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.