Let your end users share, store, and interact with a wide range of attachments, including various types of files and media. For instance, adding or updating an avatar for their profile image. For more information about the attachment feature, see Platform Manual > Attachment.
This article provides an overview of the latest HTTP API feature for handling attachments in Ditto.
To send an attachment, using multipart form, as indicated using the -F flag in your HTTP request body, make a call to the following endpoint:
Currently, the maximum upload size for attachments is limited to 1MB; however, development is in progress to increase this limit to 20MB.
Multipart form, also known asmultipart/form-data, is a standard content type used in HTTP requests to enable the transmission of various file types and media.
By flagging the attachment as multipart/form-data, you indicate that the attachment is a distinctly separate data segment within the request.
For more information, see the official Internet Engineering Task Force (IETF) Request for Comments (RFC) documentation Section 5.1 - RFC2046.
For example, the following snippet demonstrates an HTTP POST request attaching the file post.png in a form field named file:
Once invoked, the id that uniquely identifies the uploaded attachment in Ditto along with the length (len) of the uploaded attachment in bytes. For example:
To download an attachment, make a call to the following endpoint:
When invoking upsert to create a new document, if desired, include an attachment in your upsert operation by calling TypeOverride::Attachment method as follows:
In addition to including an attachment when upserting a new document, you can include an attachment when updating documents, as follows:
Simplify your code's handling of attachment-related data obtained from the endpoint by simplifying the overall format or specific field types.
To modify the overall presentation of attachments within the response from read endpoints, set formatAttachment to true.
Once set, read endpoints will return attachment response details in a human-readable format, making it easier to work with.
For example, the following snippet demonstrates including formatAttachment: true in the request payload:
Resulting in the following response that displays attachment details, such as id, len, and metadata:
To control response serialization by specific fields retrieved from the read endpoints, use serializedAs: 'latestValuesAndTypes'.
The following snippet provides an example response to serializedAs: 'latestValuesAndTypes':