none
Convert cURL to powershell (again) RRS feed

  • Question

  • Sorry All,

    I've checked previous posts on this kind of question to no avail.  We need to regularly upload a .csv file to a company that maintains a sign-in system for our offices (so staff list is kept current).  The company recommends using cURL but our security guy doesn't like the installation of non-Microsoft software so I'd like to convert the following to powershell:

    curl -F "file=@HostsFile.csv" "https://company.com.co/v1/host/csv-upload/api-key?remove-hosts=true&send-emails=false" -H "X-Sine-Api-Key: (api key)"

    Any assistance gratefully received.

    Cheers

    Wednesday, October 9, 2019 12:11 AM

Answers

  • Hey CJ

    I'm using Sine as well and have been having the same issue.

    The -F in cURL sends multipart/form-data, support for which isn't built-in to Windows PowerShell (even though you can set the content-type). I found a few StackOverflow discussions which provided workarounds involving constructing your own multipart/form-data with appropriate boundaries, but wasn't too keen on that.

    PowerShell Core 6.1 now officially supports this through a new Form parameter, which allows you to do this:

    Invoke-RestMethod -Method Post -Uri 'https://api.sine.co/v1/host/csv-upload/api-key?remove-hosts=true&send-emails=false' -Headers @{ 'X-Sine-Api-Key' = 'Tv4wMtHc7FF4YCCn' } -Form @{ file = Get-Item 'C:\Exports\HostsFile.csv' }

    (or Invoke-WebRequest if you prefer).

    This has been working really well for me so hopefully you're able to get PowerShell Core 6.1 or later installed if you haven't already, since it's still from Microsoft.

    Josh

    Tuesday, January 28, 2020 9:59 AM

All replies

  • help Invoke-WebRequest -online

    help Invoke-RestMethod -online


    \_(ツ)_/

    Wednesday, October 9, 2019 12:16 AM
  • Hey CJ

    I'm using Sine as well and have been having the same issue.

    The -F in cURL sends multipart/form-data, support for which isn't built-in to Windows PowerShell (even though you can set the content-type). I found a few StackOverflow discussions which provided workarounds involving constructing your own multipart/form-data with appropriate boundaries, but wasn't too keen on that.

    PowerShell Core 6.1 now officially supports this through a new Form parameter, which allows you to do this:

    Invoke-RestMethod -Method Post -Uri 'https://api.sine.co/v1/host/csv-upload/api-key?remove-hosts=true&send-emails=false' -Headers @{ 'X-Sine-Api-Key' = 'Tv4wMtHc7FF4YCCn' } -Form @{ file = Get-Item 'C:\Exports\HostsFile.csv' }

    (or Invoke-WebRequest if you prefer).

    This has been working really well for me so hopefully you're able to get PowerShell Core 6.1 or later installed if you haven't already, since it's still from Microsoft.

    Josh

    Tuesday, January 28, 2020 9:59 AM