PowerShell - Uploading APK files to API Packages/v2

Hello,

I have some trouble uploading an APK to the API via packages/v2.

$Header = @{}
$Header["Authorization"] = "Bearer " + $script:Token
$Header["Accept"] = "application/json"
$boundary = "mobicontrol_boundary"

$BinaryString = [IO.File]::ReadAllBytes($PathToAPK)
$LF = "`r`n"
$body = "--${boundary}${LF}"

$body += "Content-Type: application/vnd.soti.mobicontrol.packagearchive.metadata+json${LF}${LF}"
$body += '{
    "PackageName":"AutoAPP TestPackage", "PackageVersion":"1.0", "PackagePlatform":"Android",
    "PackageFiles": [{ "FileName":"Testfile.apk", "FileSourceType":"Binary" } ], 
    "PackagePrompt": { "ShowPromptBeforeInstall": false, "PromptMessage":"Install package PackageName now?", "PromptMessageTimeout":10 }
}'
$body += $LF
$body += "--${boundary}${LF}"
$body += "Content-Type: application/octet-stream${LF}"
$body += "Content-Type-Encoding: Binary${LF}"
$body += 'Content-Disposition: attachment; filename="Testfile.apk"' + $LF
$body += $LF
$body += $BinaryString
$body += "${LF}${LF}--${boundary}--${LF}"

$response = Invoke-WebRequest -Uri ("https://" + $script:MCFQDN + "/mobicontrol/api/packages/v2") -ContentType "multipart/related; boundary=$boundary" -Method POST -Headers $Header -Body $Body -Verbose

I get a success on the upload and after that, on the device, i get I/O error and when I download the package from MobiControl and unpack it, the SettingsManager apk file is 20mb instead of the original 5mb.


But if I write the $binarystring back to a file with set-content, I get a 5mb file.

An example write-out of $body where I have limited the binarystring output to 100 for readability:

--mobicontrol_boundary
Content-Type: application/vnd.soti.mobicontrol.packagearchive.metadata+json

{
        "PackageName":"AutoApp TestPackage", "PackageVersion":"1.0", "PackagePlatform":"Android",

        "PackageFiles": [{ "FileName":"Testfile.apk", "FileSourceType":"Binary" } ],
        "PackagePrompt": { "ShowPromptBeforeInstall": false, "PromptMessage":"Install package PackageName now?", "PromptMessageTimeout":10 }
    }

--mobicontrol_boundary
Content-Type: application/octet-stream
Content-Type-Encoding: Binary
Content-Disposition: attachment; filename="Testfile.apk"

80 75 3 4 0 0 0 0 8 0 33 8 33 2 62 29 233 132 51 0 0 0 56 0 0 0 57 0 0 0 77 69 84 65 45 73 78 70 47 99 111 109 47 97 110 100 114 111 105 100 47 98 117 105 108 100 47 103 114 97 100 108 101 47 97 112 112 45 109 101 116 97 100 97 116 97 46 112 114 111 112 101 114 116 105 1
01 115 75 44 40 240 77 45 73 76 73 44 73 12 75 45

--mobicontrol_boundary--

So, do anyone have any idea as to why the apk file when received by MobiControl gets bigger/corrupt?

Thanks in advance

a year ago
SOTI MobiControl
ANSWERS
T
TLMOD@SOTI
a year ago

Hi Mikael,

Thank you for posting on SOTI pulse.

The script investigation requires thorough testing on devices and a detailed analysis. Therefore, I would recommend that you log a case so the support team can look into this deeply.