multipart/form-data

multipart/form-data is the media type browsers use when a form includes a file input, and the conventional way to upload files to an API. The body is divided into parts, each with its own headers — a name, optionally a filename and a Content-Type — so text fields and binary files travel together in one request.

How it works

The Content-Type header declares a boundary string, and the body is the parts separated by that boundary. Clients should let their HTTP library build the body and set the header, boundary included; servers check each part's declared type and size, answering 415 for a file type they do not accept and 413 for one that is too large.

The misconception

That setting Content-Type: multipart/form-data by hand is helpful. Without the boundary parameter the server cannot split the body, and the upload fails in a confusing way. With fetch and FormData, the right move is to not set the header at all.

Try it here

POST /movies/v1/movies/{id}/poster simulates a multipart poster upload, answering 415 for an unsupported image type and 413 for an oversized one — the two checks every upload endpoint needs.

Sources

Other terms

All glossary terms · Testing techniques · All 39 mock REST APIs

Last updated