AnsweredAssumed Answered

How to upload a file via API using Powershell?

Question asked by Benjamin Selby on May 27, 2020
Latest reply on Jun 2, 2020 by Benjamin Selby

I'm trying to upload a file via the API using Powershell. I am an administrator with 'impersonate' authorisation. I have tried following the API documentation for this process but am having trouble putting it into practice. 

 

My understanding is that the first step is to notify of a pending file upload and obtain a target URL: 

 

Invoke-WebRequest -headers $headers -method POST -URI https://<URL>:443/api/v1/folders/<FOLDER_ID>/files

 

This generates the following response: 

 

file_param : file
progress :
upload_url : https://inst-fs-syd-prod.inscloudgate.net/files?token=<TOKEN>
upload_params : @{filename=; content_type=}

 

So, my understanding is that I now POST the file contents to that UPLOAD_URL, right? Except that I can't seem to get anything to work. I have tried the following: 

 

$uri = '<UPLOAD_URL>'

$body = @{
   'filename'='<FILE_NAME>;
   'content_type'='text/plain';
   'file'=[IO.File]::ReadAllBytes('<FILE_PATH>')}

Invoke-RestMethod -uri $uri -method POST -body $body

 

I receive the following response: 

 

Invoke-RestMethod: {"error":"No #file uploaded"}

I have tried various other permutations of command configurations, but can't get anything to work. Can anyone help, please? 

 

If there is no way of doing this with Powershell, maybe somebody can provide a method using a different language? I'd be interested in any input. 

 

Thank you. 

Outcomes