0

I am trying to upload a Zip file via RestAPI in powershell and Powershell version I am using is 5.1.

since Invoke-restmethod in powershell 5.1 doesn't have -form option,I tried to run curl command in CMD using powershell.When I run it locally it's working correctly. But when i try to run it in a server(azure devops agent) I am getting error saying curl command is not recognized.

What am i missing here it's runs as expected when i try in vs code, but not when i run it from a server.

$header1 = "accept: application/json"
$header2 = "X-Authorization: $($token)"
$header3 = "Content-Type: multipart/form-data"
$body1 = "upload=@Name.zip;type=application/x-zip-compressed"
$body2 = "actionIfExisting=Existing"
$body3 = "publicWorkspace=Public"
cmd.exe /c curl -X POST $uri -H $header1 -H $header2 -H  $header3 -F $body1 -F $body2 -F $body3
mklement0
  • 245,023
  • 45
  • 419
  • 492
Sar
  • 159
  • 1
  • 8
  • 3
    Is `curl.exe` in a directory specified in the PATH variable when this is run? – lit Apr 28 '21 at 16:33
  • 2
    You don't need to use `cmd` for that. You can run `curl` directly from Powershell with `curl.exe` instead of `curl` which is the alias for `Invoke-WebRequest` – Nico Nekoru Apr 28 '21 at 17:44
  • At least recent Windows 10 and Windows Server versions come with `curl.exe`, and its full path is `C:\Windows\system32\curl.exe` - given that a system probably won't work properly if ``C:\Windows\system32` _isn't_ listed in the `PATH` environment variable (`$env:PATH` in PowerShell), the likely explanation is that the server running the script doesn't have this executable. – mklement0 Apr 28 '21 at 17:48
  • 1
    @mklement0 I was using windows server 2016 – Sar Apr 29 '21 at 05:14
  • @NicoNekoru In powershell 5.1 invoke-webrequest doesn't have a option straight option to upload file – Sar Apr 29 '21 at 05:15
  • Sar, what @NicoNekoru is trying to tell you is that if you invoke `curl.exe` _with the `.exe` extension explicitly specified_, PowerShell's `curl` alias (for `Invoke-WebRequest`) doesn't interfere and that there is therefore no need to call it via `cmd.exe /c`. This, of course, assumes that `curl.exe` is in your system's PATH, and the fact that it isn't is your real problem. – mklement0 Apr 29 '21 at 07:24

1 Answers1

1

Luckily I had git installed in the server and I read in this stack overflow page that Git comes with preInstalled curl.exe. So I called the path where git has curl.exe and using & operator in powershell to execute the curl.exe directly in powershell itself.

& 'C:\users\git\mingw64\bin\curl.exe' -X POST $uri -H $header1 -H $header2 -H  $header3 -F $body1 -F $body2 -F $body3
mklement0
  • 245,023
  • 45
  • 419
  • 492
Sar
  • 159
  • 1
  • 8
  • Good to know. Just an aside re syntax: You need `&` here because your executable path is _quoted_; however, given that your particular path doesn't _need_ quoting, you could also do `C:\users\git\mingw64\bin\curl.exe-X POST ...`; see [this answer](https://stackoverflow.com/a/57678081/45375) for more information. – mklement0 Apr 29 '21 at 07:19
  • Re default availability of `curl.exe`, located at `C:\Windows\system32\curl.exe` and therefore in the system's PATH: requires Windows Server _2019_ or above (you mentioned that you're using _2016_). – mklement0 Apr 29 '21 at 07:48