Unlike most Web API methods, endpoints that accept an uploaded file do not use
the standard JSON structure or the standard Content-Type value of
application/json. Instead, requests to these endpoints must use a
Content-Type of multipart/form-data.
When using this content type, a special boundary parameter must be included in the
Content-Type declaration. This boundary value must not appear anywhere within the
headers or contents of the submitted file, so a GUID or randomly-generated string is recommended.
Most HTTP client libraries handle this automatically.
The request body contains the file's headers and data wrapped within boundary delimiters. The
file's headers must include a Content-Disposition header indicating
a value of form-data and the name of the file. A Content-Type header
must also specify the MIME type of the file. These headers are followed by a blank line and the
contents of the file.
For more information on how to structure these calls, see RFC 7578.
The example below uses an arbitrarily-generated boundary value of CHANGEME to delimit
the beginning and end of the submitted file. Note that both delimiters have a leading
-- and that the end delimiter also has a trailing --.
Between those delimiters are two headers indicating the disposition of the content (including the
filename) and the content type (as a generic application/octet-stream binary file),
followed by a blank line and the actual file contents.
POST /api/items/1/attachments HTTP/1.1 Content-Type: multipart/form-data; boundary=CHANGEME --CHANGEME Content-Disposition: form-data; name="aeneid.txt"; filename="aeneid.txt" Content-Type: application/octet-stream FORSAN ET HAEC OLIM MEMINISSE IUVABIT --CHANGEME--