2

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.

5
  • Do you really repeat headers: headers: as shown? Commented Aug 4, 2018 at 11:07
  • No, thanks for catching it. Commented Aug 4, 2018 at 11:30
  • Did you try to use Headers class? example Commented Aug 4, 2018 at 11:33
  • The Headers class has the same behavior. Commented Aug 4, 2018 at 12:00
  • Possible duplicate of stackoverflow.com/questions/43842793/… Commented Aug 5, 2018 at 8:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.