Transactional upload is a pattern for file uploads. The pattern allows the document publisher to send multiple files to the server in one transaction.
Transactional Upload is a three-step process in which the document publisher
- initiates an ‘upload session’
- uploads the files
- commits the files for further processing
Let me describe each step in the following example, where a document publisher creates a case to which he attaches some documents.
1. The document publisher initiates an upload session
Endpoint: POST /cases
The document publisher initiates a new case and includes the metadata in the body of the request:
POST /cases HTTP/1.1
Host: api.domain.com:443
Authentication: Bearer some-jwt-token
Content-Type: application/json
"collection": "images",
"subject": "Car accident",
"createdAt": "2020-03-13T07:30:52Z",
"type": "CLAIM"
The API provider answers with HTTP response code ‘201 Created’ and includes the link to the newly created case in the Location header of the response, which includes the case ID.
HTTP/1.1 201 Created
Cache-Control: no-cache
Content-Type: application/json; charset=utf-8
Location: https://api.domain.com/cases/35c9-402f-afd6-7153cdb7b1be
2. The document publisher uploads files for a case
Endpoint: PUT /cases/caseID/documents/fileName
The document publisher uploads all the documents related to a case one by one via direct file upload. The case ID and the filename are specified as path parameters.
PUT /cases/35c9-402f-afd6-7153cdb7b1be/documents/some-filename.jpg HTTP/1.1
Host: api.domain.com:443
Authentication: Bearer some-jwt-token
Content-Type: image/jpeg
Content-Length: 124
<Your-file-contents-is-posted-in-the-request-body>
The response contains a link to the newly created file in the Location response header:
HTTP/1.1 201 Created
Cache-Control: no-cache
Content-Type: application/json; charset=utf-8
Location: https://api.domain.com/cases/35c9-402f-afd6-7153cdb7b1be/documents/some-filename.jpg
3. The document publisher commits the case (session) for further processing
Endpoint: POST /cases/caseID/commit
Once all files have been uploaded to the server for a case, the document publisher can commit the files for further processing by the backend service.
The files will be processed asynchronously by a backend service and the API answers with HTTP response code “202 Accepted”.
Pros:
- Developer (DX) friendly
- Enables the ability to send multiple files in one batch
Cons: