Submitting Files

Unlike most web API methods, web API endpoints that accept an uploaded file do not use the standard JSON structure (and by extension, the standard Content-Type value of application/json). Instead, submissions to web API endpoints that accept an uploaded file must use a Content-Type of multipart/form-data.

When declaring a request with this content type, a special boundary parameter must be included in the Content-Type declaration. This value of this boundary must not occur within the headers or contents for the submitted file, and therefore a value such as a GUID or a randomly-generated string should be used as part of your boundary. Most HTTP client libraries will handle this for you.

Structure of File Submission Calls

The body of the request contains these boundaries and the file's headers and data within those boundaries. The file's headers must include a Content-Disposition header which indicates a value of form-data and the name of the file. In addition, a Content-Type header must 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 and format these types of calls, see RFC 7578.

Example File Submission

The submission below uses an arbitrarily-generated boundary value of "CHANGEME" to delimit the beginning and end of the submitted file. Note that both beginning and end delimiters have a leading "--" string and that the end delimiter contains a trailing "--".

Between those boundary 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 contents of the file.

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--