Search code examples
javascriptapi

Logic for POST call in multipart/form-data


I want to make a POST call from my end with

"Content-Type", "multipart/form-data; boundary=--------------------------"RANDOM_24_DIGIT_NUMBER.

I am unable to deduce the logic for generating the boundary and getting 400 as response.
I am utilizing JS.

The response is 200 when done via POSTMAN, however I want to achieve it within my code.
Can someone help me with the logic that POSTMAN utilizes to generate this number.

Headers when done via POSTMAN:

Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: multipart/form-data; boundary=--------------------------237519436009365388953211
Content-Length: 2749

Connection: keep-alive
Content-Type: multipart/form-data; boundary=--------------------------214502465121174114780437
Content-Length: 2749

Solution

  • It's strange that you're getting an error. Normally, a boundary can be almost anything (see RFC2046).

    But you shouldn't generate a boundary manually. Use FormData instead. It generates Content-Type automatically. See an example on MDN.