Top community members

Welcome to Dirask IT community! ❀ đŸ’»
We are community of people that helps each other.

If you think you have some skills to help others

help someone and become a part of our community - List Q & A

How to get or set boundary in multipart/form-data from FormData?

5 points
Asked by:
JustMike
3376

As in topic. I would like to set up my own boundary parameter for Content-Type. when i work with FormData class in JavaScript.

Expected header:

Content-Type: multipart/form-data; boundary=------some-random-characters

My code looks folowing way:

const requestData = new FormData();
requestData.append('file', file); // file from File API

const response = await fetch('/backend/upload', {
    method: 'POST',
    headers: {
        'Content-Type': 'multipart/form-data; boundary=------some-random-characters',
        'Accept': 'application/json'
    },
    body: requestData
});
var responseData = await response.json();

When I execute code, header Content-Type is correct, but in body I have ------WebKitFormBoundary2lZSUsxEA3X5jpYD instead of ------some-random-characters.

Request screenshot from DevTools:

Any idea how to get or set boundary for FormData?

1 answers
2 points
Answered by:
JustMike
3376

At this moment there is no way to set up boundary for FormData.

Just remove: 'Content-Type': 'multipart/form-data; boundary=------some-random-characters' - it will casue the Content-Type will be set according to body type.

Fixed code:

const requestData = new FormData();
requestData.append('file', file); // file from File API

const response = await fetch('/backend/upload', {
    method: 'POST',
    headers: {
        'Accept': 'application/json'
    },
    body: requestData
});
var responseData = await response.json();
0 comments Add comment

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more