1

What is wrong with the following command? I am running curl.exe in powershell

PS C:\Users\manuchadha> curl.exe 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" }'
{
  "error": {
    "message": "Your request contained invalid JSON: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)",
    "type": "invalid_request_error",
    "param": null,
    "code": null
  }
}
curl: (6) Could not resolve host: me
curl: (6) Could not resolve host: a
curl: (6) Could not resolve host: funny
curl: (3) unmatched close brace/bracket in URL position 7:
story }
mklement0
  • 382,024
  • 64
  • 607
  • 775
Manu Chadha
  • 15,555
  • 19
  • 91
  • 184
  • 2
    The sad reality up to PowerShell 7.2.x is that an _extra, manual_ layer of ``\``-escaping of embedded `"` characters is required in arguments passed to _external programs_. This has been fixed in PowerShell 7.3,0 with selective exceptions on Windows. See the [proposed duplicate](https://stackoverflow.com/a/66837948/45375) for details. – mklement0 Jun 01 '23 at 11:54

2 Answers2

1

It's not clear to me exactly why this is happening (and I think it's probably a bug somewhere in the stack) but it seems to be a Powershell-specific thing, not a curl-specific thing. I wrote a little test program that looks like this:

using System;

namespace argvtest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            int i = 0;
            foreach (var arg in args) {
                Console.WriteLine(i++ + ": " + arg);
            }
        }
    }
}

After that, I tried running your command as stated, but through my test program:

PS C:\Users\PervonZweigbergk\repos\argvtest\bin\Debug\netcoreapp3.0> .\argvtest.exe "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" }'
Hello World!
0: https://aoai-try-mc.openai.azure.com/openai/deployments/try-davinci/completions?api-version=2022-12-01
1: -H
2: Content-Type: application/json
3: -H
4: api-key: keygoeshere
5: -d
6: {prompt: tell
7: me
8: a
9: funny
10: story }
PS C:\Users\PervonZweigbergk\repos\argvtest\bin\Debug\netcoreapp3.0>

As we can see, for some reason, what you're intending to have as a single argument (your JSON payload as the -d argument) at some point ends up being split into multiple arguments and the "'s removed.

A workaround to this seems to be to double the quotes:

PS C:\Users\PervonZweigbergk\repos\argvtest\bin\Debug\netcoreapp3.0> .\argvtest.exe "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"" }'
Hello World!
0: https://aoai-try-mc.openai.azure.com/openai/deployments/try-davinci/completions?api-version=2022-12-01
1: -H
2: Content-Type: application/json
3: -H
4: api-key: keygoeshere
5: -d
6: {"prompt": "tell me a funny story" }
PS C:\Users\PervonZweigbergk\repos\argvtest\bin\Debug\netcoreapp3.0>

I'm sure there's an interesting reason as to why the command line handling seems "broken" like this, but I at least hope this helps answer your specific question.

Edit: This previous Stack Overflow answer may provide some insight: PowerShell stripping double quotes from command line arguments

  • The sad reality up to PowerShell 7.2.x is that an _extra, manual_ layer of ``\``-escaping of embedded `"` characters is required in arguments passed to _external programs_. This has been fixed in PowerShell 7.3,0 with selective exceptions on Windows. – mklement0 Jun 01 '23 at 11:55
1

For completeness, escaping " helped -

curl.exe 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: ea0456eef14d48cba8c0383ccfd124fe" -d '{ \"prompt\": \"tell me a funny story\" }'
Manu Chadha
  • 15,555
  • 19
  • 91
  • 184
  • Yes, but please consider closing your question as a duplicate as proposed, because the linked post has important background information. – mklement0 Jun 01 '23 at 15:32