I'm writing a python script to automate the process of uploading our packages to SOTI. However, I am having difficulty sending the request to upload an Android APK file. here is my code for reference;
file_name = 'com.aviagen.trapping.apk'
encoded_file = ''
with open(file_name, 'rb') as f:
data = f.read()
encoded_file = base64.b64encode(data)
encoded_file = str(encoded_file, "utf-8")
boundary = "mobicontrol_boundary"
#part 1 - body parameters
body = f"--{boundary}\r\n"
body = body + f"Content-Type: application/vnd.soti.mobicontrol.package.metadata+json\r\n\r\n"
body = body + "{ 'DeviceFamily': 'AndroidPlus' }\r\n"
#part 2 - file details
body = body + f"--{boundary}\r\n"
body = body + f"Content-Type: application/vnd.soti.mobicontrol.package\r\n"
body = body + f"Content-Type-Encoding: base64\r\n"
body = body + "Content-Disposition: attachment; filename='com.aviagen.trapping.apk'\r\n"
body = body + "\r\n"
#part 3 - file content
body = body + encoded_file
body = body + f"\r\n--{boundary}--\r\n"
print(body)
body = body.encode('utf-8')
request = urllib.request.Request(f'{BASE_URL}/packages', data=body)
request.add_header('Authorization', f'Bearer {token}')
request.add_header('Content-Type', 'multipart/related; boundary=mobicontrol_boundary')
response = urllib.request.urlopen(request)
data = json.loads(response.read())
response.close()
This is resulting in an error - 422 (Unprocessable Entity).
I have checked the file and can confirm that it is a valid signed apk which installs on my device. Is there something wrong with my request body? I saw a couple other examples of Package uploading, however they all use Powershell and I would very much like to avoid powershell at all costs.