0

I'm trying to send an http POST request using the JavaScript Fetch API. What I've got so far looks like this (in TypeScript notation):

  const fetchHeaders = new Headers();
  fetchHeaders.append("Content-Type", "multipart/form-data; boundary=???");

  const fetchOptions: RequestInit = {
        method: "Post",
        body: this.file,
        headers: fetchHeaders
  }

  await fetch("uri", fetchOptions);

Assuming that this.file is either a .xls or .xlsx Excel file uploaded using a <input type="file">, how do I find out the boundary to change boundary=???. When doing this in POSTMAN, the boundary is automatically set and changes for every request.

Edit:

The duplicate question is nowhere close to the same as this one. I tried to cannibalize the accepted answer to suite my needs, but that creates a different Request/Response and therefore the boundary from that method is different than the one that the fetch() actually sends.

  • Why do you need the `boundary` of `multipart/form-data`? – guest271314 Oct 27 '17 at 16:14
  • Everything I've read online has mentioned that `multipart/form-data` requires boundaries and this code gets a 500 status code response from my api when it's just `append("Content-Type", "multipart/form-data")`. That combined with POSTMAN appending the boundary has led me to believe that it's required. – Casey Cummings Oct 27 '17 at 16:24
  • `500` is server error code, yes? – guest271314 Oct 27 '17 at 16:39
  • It is, and it makes sense if the server throws an error because it doesn't know how to parse the multipart file if it's not given the boundary. I think I can pull the boundary from the answer to that question that was marked duplicate, though. Should've linked it, though, because it's not duplicate. – Casey Cummings Oct 27 '17 at 17:10
  • The actual question "How to get the content-type boundary". Not certain why it is necessary to set `Content-Type` header in the first instance? – guest271314 Oct 27 '17 at 17:18
  • The code in the API to read the file enforces the need for `Content-Type="multipart/form-data"` and `boundary="???"` in order to read the file. – Casey Cummings Oct 27 '17 at 20:21

0 Answers0