curl.exe命令在powershell上不起作用[重复]

wwodge7n  于 2023-06-23  发布在  Shell
关注(0)|答案(2)|浏览(179)

此问题已在此处有答案

Powershell: passing json string to curl(5个答案)
20天前关闭。
下面的命令有什么问题?我在powershell中运行curl.exe

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 }
yzckvree

yzckvree1#

我不清楚为什么会发生这种情况(我认为这可能是堆栈中某个地方的错误),但这似乎是Powershell特有的事情,而不是curl特有的事情。我写了一个小测试程序,看起来像这样:

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);
            }
        }
    }
}

在那之后,我试着运行你的命令,但是通过我的测试程序:

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>

正如我们所看到的,出于某种原因,您打算作为单个参数的内容(您的JSON有效负载作为-d参数)在某些时候最终被拆分为多个参数,并且“被删除了。
解决这个问题的方法似乎是将引号加倍:

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>

我相信这是一个有趣的原因,为什么命令行处理看起来像这样“破碎”,但我至少希望这有助于回答您的具体问题。
编辑:之前的Stack Overflow回答可能会提供一些见解:PowerShell stripping double quotes from command line arguments

tv6aics1

tv6aics12#

为了完整性,转义"有助于-

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\" }'

相关问题