curl.exe命令在PowerShell上不起作用。

huangapple go评论78阅读模式
英文:

curl.exe command not working on 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": "您的请求包含无效的 JSON:期望属性名用双引号括起来:第 1 行第 2 列 (字符 1)",
    "type": "invalid_request_error",
    "param": null,
    "code": null
  }
}
curl: (6) 无法解析主机: me
curl: (6) 无法解析主机: a
curl: (6) 无法解析主机: funny
curl: (3) URL 位置 7 处的大括号/方括号不匹配:
story }
英文:

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 }

答案1

得分: 1

对我来说,目前尚不清楚为什么会发生这种情况(我认为这可能是堆栈中某处的错误),但似乎与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>

正如我们所看到的,由于某种原因,您打算将作为-d参数的JSON有效载荷拆分为多个参数,并移除了双引号。

解决此问题的方法似乎是将引号加倍:

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答案可能会提供一些见解:https://stackoverflow.com/questions/6714165/powershell-stripping-double-quotes-from-command-line-arguments

英文:

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: https://stackoverflow.com/questions/6714165/powershell-stripping-double-quotes-from-command-line-arguments

答案2

得分: 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" }'

英文:

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

huangapple
  • 本文由 发表于 2023年6月1日 15:26:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379566.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定