1

The following curl command works in azure cli.

curl https://aoai-try-mc.openai.azure.com/openai/deployments/try-davinci/completions?api-version=2022-12-01 -H "Content-Type:application/json" -H "api-key:keygoeshere" -d '{"prompt":"Tell me a funny story"}'

I have created an equivalent on for powershell on desktop but it fails. What am I doing wrong?

PS C:\Users\manuchadha> curl https://aoai-try-mc.openai.azure.com/openai/deployments/try-davinci/completions?api-version=2022-12-01 -H @{"Content-Type"="application/json" ; "api-key"="keygoeshere"} -Body @{"prompt"="Tell me a funny story"}
curl : {"error":{"code":"404","message": "Resource not found"}}
At line:1 char:1
+ curl https://aoai-try-mc.openai.azure.com/openai/deployments/try-davi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Manu Chadha
  • 15,555
  • 19
  • 91
  • 184
  • In short: In _Windows PowerShell_ (but no longer in _PowerShell (Core) 7+_) `curl` is built-in alias of `Invoke-WebRequest`, whose syntax is very different from that of `curl.exe`. To reliably invoke the latter (on Windows), invoke it _with `.exe`_ (`curl.exe ...`) – mklement0 Jun 01 '23 at 11:50

1 Answers1

5

Powershell provides an alias for "curl" that instead makes it run invoke-webrequest which basically never does the right thing for you and will mostly just ruin whatever you try to do.

An easy way to circumvent this craziness is by typing out curl.exe instead of just curl on the command line.

Windows 10 and 11 have shipped a bundled "real" curl tool since several years by now.

Daniel Stenberg
  • 54,736
  • 17
  • 146
  • 222
  • Thanks. I feel the pain. Which then brings me to problem with `curl.exe`. can you take a look at https://stackoverflow.com/questions/76379566/curl-exe-command-not-working-on-powershell please – Manu Chadha Jun 01 '23 at 08:27
  • 1
    In your $PROFILE: `Remove-Item Alias:\curl -ErrorAction SilentlyContinue` – patthoyts Jun 01 '23 at 08:29
  • 1
    Removing the curl alias seems like a good idea (it was a terrible idea to put it in to start with) but will, unfortunately, break any scripts that rely on the "curl" alias to invoke-webrequest to be there. Best practice when writing scripts is to not rely on aliases, precisely for this kind of reason, unfortunately not everyone writes scripts to best practices. Removing this alias is, therefore, setting yourself up for pain in the future. – Per von Zweigbergk Jun 01 '23 at 10:13
  • @PervonZweigbergk what i don't understand is, how did the alias pass codereview? was there any codereview at all during the development of PowerShell? how could they find 2 people to agree the alias was a good idea? – hanshenrik Jun 02 '23 at 10:06
  • @hanshenrik I'm not sure exactly how early, but the curl -> Invoke-Webrequest alias has been in Powershell for a very long time. I wouldn't be surprised if it predates Powershell 1.0. Decisions taken that early in a project (especially in an unofficial project like Powershell started out as) are not neccesarrilly subject to code review. – Per von Zweigbergk Jun 02 '23 at 12:19