2

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 populate the boundary= part of the Content-Type header dynamically when the fetch is called?

When doing this same operation in POSTMAN, the boundary is automatically set and I can't figure out how.

1 Answers1

1

I figured out the core issue - passing the file itself as the body. This was how I solved it:

const formData = new FormData();
formData.append("file", this.file, this.file.name);

const fetchOptions: RequestInit = {
    method: "Post",
    body: formData
}