API file upload

How to upload files using the eLabAPI

File upload using the eLabAPI

The eLabAPI allows you to upload files to the eLab environment. This document will explain how to upload files using the eLabAPI. The main step here is encoding the file to a format that can be sent to the request body. Important to note is that the file size cap is 150mb.

The following recipe shows how a file can be uploaded to an experiment section of one of your experiments. For this, the following four variables are needed:

  • baseurl: the base URL of the eLab environment that you are on.
  • expJournalId: the ID of the experiment section where you want to upload the file.
  • fileToUpload: the path to the file you want to upload from your local machine.
  • token: Your eLabAPI token.

The example in this recipe can serve as a base for your implementation when uploading a file(s) to the eLab environment.

File content encoding

The file content must be sent as raw binary data. If the file content is read or converted as a string before sending, the result is a silently corrupted file: the API stores whatever bytes it receives and always returns a success response, so the corruption only becomes apparent when trying to open the downloaded file.

Python 2 is a known cause of this. In Python 2, str and bytes are the same type, meaning binary file content can silently pass through a text-oriented encoding step without error. Python 3 enforces a strict separation between the two, making such mistakes an explicit error rather than a silent one.