How Can We Help?
Pure API: Uploading files Pure API: Uploading files
Introduction
A common mistake when uploading files to Pure is bad usage of mime type headers.
Multipart file upload is not supported in the Pure write API
Pure currently only supports uploading one file at a time, with a specific mime type. Pure does not understand upload data with content-type
set to multipart/....
Code libraries (like Python requests) support uploading both raw files and multipart. It also supports manually setting the content-type on a PUT
request to e.g. image/jpeg
.
Requirements
- Pure Admin Account
- Valid API Key
- API Documentation
- Python
Expertise Level
Knowledge of Python is required. See also Python API Requests: Fundamental Coding Examples
Examples
There are three typical combinations of doing this (examples shown below):
Example 1: WORKS.
In this example, we set the content-type
header to image/jpeg
on the put request and add the raw image binary data, using the data
parameter in the requests.put(...)
function.
Example 2: DOES NOT WORK
In this example we do not set a header on the PUT
request and we are using the files
parameter in the requests.put(...)
function. In this case, the requests
library will automatically add the multipart/...
header. Pure will see this header and return an HTTP error code 400 with an error description.
Example 3: DOES NOT WORK.
In this example we set the content-type
header to image/jpeg
on the PUT request and add multipart data, using the files parameter in the requests.put(...)
function. In this case, the requests library will not automatically set the content-type
to multipart/...
, as it is manually set. Pure will accept the content-type
header image/jpeg
and upload the binary data as an image. Since the multipart/...
header data is included in the data, the file is unable to be shown as an image.
Only example 1 works. Example 2 will fail with an error message when trying, but example 3 will fail silently and upload a corrupted image file.
Python examples
The three examples mentioned in the section above are shown here, written in Python.
These examples are just that, examples. They must be used at your own risk, and Elsevier will not support or review code that is using this or is based on this.
import requests
photo_file_path = "../resources/upload_test.jpg"
with open(photo_file_path, 'rb') as file:
################################################################################
# Example 1: Works. Binary file upload.
headers = {
"accept": "application/json",
"api-key": "insert here",
"content-type": "image/jpeg"
}
response = requests.put("<server URL>/ws/api/persons/file-uploads", headers=headers, data=file)
print("Response code:", response.status_code)
print("Response text:", response.text)
################################################################################
# Example 2: DOES NOT WORK. It will return error code 400 with an error description.
headers = {
"accept": "application/json",
"api-key": "insert here"
# Omitting the multipart/form-data header, as the "requests" library will automatically add it.
}
files = {'file': (photo_file_path, file, 'image/jpeg')} # Multipart specification
response = requests.put("<server URL>/ws/api/persons/file-uploads", headers=headers, files=files)
print("Response code:", response.status_code)
print("Response text:", response.text)
################################################################################
# Example 3: DOES NOT WORK. The request will go through as mime type is forced to be "image/jpeg", but the uploaded
# JPEG file will be corrupted, as the multipart header (that Python adds when using "files=...") will be considered part
# of the binary data of the image.
headers = {
"accept": "application/json",
"api-key": "insert here",
"content-type": "image/jpeg" # Forcing the mimetype to "image/jpeg".
}
files = {'file': (photo_file_path, file, 'image/jpeg')} # Multipart specification
response = requests.put("<server URL>/ws/api/persons/file-uploads", headers=headers, files=files)
print("Response code:", response.status_code)
print("Response text:", response.text)
Updated at January 20, 2025