How to upload a file using Powershell to a web app with API

Hi there,

Hope you well!

So we are testing around Octopus and trying to setup the deployment steps with a third party SaaS product. Disclaimer I am good with C#, know a little bit Python but nothing about powershell. But this task has to be done in Powershell (team’s decision).

Here are some steps:
1: Login with third party apps using basic authentication
2: while login, upload a file with the third party’s API, in the header we do the basic authentication, in the body we have to upload the file, setup some parameters to false/true value.
refer: REST Application Package API · Transact Services Guide (TSG)


3: here we pause till confirm - there is a manual step we have to backup a package
4: Now we call the third party’s API, to get the app package archive. And move this .zip file to a network folder.
refer: REST Application Package API · Transact Services Guide (TSG)
5: We call a service API to start the deployment. this is the in house API Dev team created. This step will setup some parameters to false/true value.
6: here we pause till confirm. Sometimes there is a manual step to upload some files.
7: We call a service API to update forms attributes. this is the inhouse API Dev team created. This step will setup some parameters to false/true value.
8: End of deployment. Wait for Dev team to confirm.

As you might find it is mostly Post/Get method using Invoke-RestMethod ( I assumed), I am wondering if you have some example code or tips while uploading files (size less than 50M). And I am not quite sure if i have to worry about async or not.

Thank you so much!
Rachel

Hi @rachel.dong

Thanks for getting in touch.

Because you want to upload both a file and set some parameters you’ll be uploading multipart/form-data. Unfortunately, PowerShell on Windows (known as Windows PowerShell or Desktop) doesn’t have a great story around uploading multipart/form-data natively using either Invoke-WebRequest or Invoke-RestMethod.

As such, there will be limited options available to you since you need to use PowerShell. I’ve outlined some options:

  1. Use PowerShell Core. If you can use this version of PowerShell, this would be the best option. You could use the -Form parameter on Invoke-RestMethod which was added in PowerShell Core 6.1 which can be used to add both file content and parameters. See the Microsoft documentation for more information. It shows an example input to the -Form parameter, which I’ve copied below for convenience:

    $Form = @{ tags = 'Vacation', 'Italy', '2017' pictures = Get-ChildItem 'c:\Users\jdoe\Pictures\2017-Italy' 
    }
    
  2. Use regular PowerShell but use the System.Net.HttpClient. You can see an example here which uploads a file (a package) to the Octopus API - OctopusDeploy-Api/PushPackage.ps1 at master · OctopusDeploy/OctopusDeploy-Api · GitHub. Note this doesn’t include setting parameters too. You would need to modify this code to add the additional parameters as item(s) in the multipart/form-data content.

It’s possible you might find a solution on the internet that does multipart/form-data using PowerShell.

I hope that helps.

Best regards,
Mark

1 Like

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.