I have the following piece of JavaScript code which works. It makes a request and the Content-Type header is set to something like multipart/form-data; boundary=----WebKitFormBoundary7LXmdzV4exnc4Fel
as one would expect from this question.
const formData = new FormData();
formData.append('value', 12);
const options = {
method: 'POST',
body: formData ,
// Uncomment to make it fail
//headers: { "authentication": "something" }
};
fetch('http:localhost:5001/images/createFromFile', options);
However, if you comment in the line with the authentication header, it stops working and the content type header is no longer set. I'm not sure if this is an error or just the way that the fetch api works.
I can't avoid the authentication header since it is added by my infrastructure code and besides, it is needed to make an authenticated request. Is there any way to make this work, either by letting the header be generated automatically or by manually setting the content type header and get the form boundary key actually match the request body.? I need the solution to use the Fetch api if at all possible, to work with existing logging and authentication.
Update: It turned out that the error I have been looking for must be in the api that wraps the fetch api, since it actually works as expected. I was being thown off by the CORS behavior of making an OPTIONS request when calling a different domain (the test script was running in a local html rather than being served by my localhost server). Therefore, this question is now pointless.