英文:
How to properly write CURL request in C++ inside system() function? (for OpenAI API calling)
问题
我现在有一个问题,尝试从`system()`函数内部发送一个CURL请求到OpenAI API。我无法使用额外的库,比如libcurl,所以我尝试使用控制台请求来实现这个目标。
这是我的代码:
```c++
int main() {
std::string command = "curl -X POST -H \"Content-Type: application/json\" -H \"Authorization: Bearer API_KEY\" -d \"{\"prompt\": \"Hello, World!\", \"max_tokens\": 5}\" https://api.openai.com/v1/engines/davinci-codex/completions\"";
int result = system(command.c_str());
return 0;
}
但我得到这个错误:
curl: (3) unmatched close brace/bracket in URL position 22:
World!, max_tokens: 5}
我应该如何正确格式化我的命令字符串?
我尝试使用CURL请求的-g
属性,但这也不起作用。
即使我成功运行代码,我还会收到来自OpenAI的另一个错误:
{
"error": {
"message": "We could not parse the JSON body of your request. (HINT: This likely means you aren’t using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON. If you have trouble figuring out how to fix this, please send an email to support@openai.com and include any relevant code you’d like help with.)",
"type": "invalid_request_error",
"param": null,
"code": null
}
}
<details>
<summary>英文:</summary>
I have a bit of a problem right now with sending a CURL request for OpenAI API from inside the `system()` function. I can't use additional libraries for this goal such as libcurl, so I am trying to do this with a console request.
This is my code:
```c++
int main() {
std::string command = "curl -X POST -H \"Content-Type: application/json\" -H \"Authorization: Bearer API_KEY\" -d \"{\"prompt\": \"Hello, World!\", \"max_tokens\": 5}\" https://api.openai.com/v1/engines/davinci-codex/completions";
int result = system(command.c_str());
return 0;
}
But I get this error:
curl: (3) unmatched close brace/bracket in URL position 22:
World!, max_tokens: 5}
How should I properly format my command string?
I tried using the -g
property for the CURL request, but this doesn't work, either.
Even if I somehow succeed to run the code, I get another error but from OpenAI:
{
"error": {
"message": "We could not parse the JSON body of your request. (HINT: This likely means you aren’t using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON. If you have trouble figuring out how to fix this, please send an email to support@openai.com and include any relevant code you’d like help with.)",
"type": "invalid_request_error",
"param": null,
"code": null
}
}
答案1
得分: 1
不要在会被Shell解释的字符串周围使用"
。像[tag:bash]这样的Shell会将!
在Hello, World!
中解释为事件。相反,请在发送给Shell的字符串周围使用'
。
此外,使用原始字符串文字(列表中的第6点),无需转义特殊字符。
示例:
#include <cstdlib>
#include <iostream>
int main() {
std::string command =
R"aw(curl -X POST -H 'Content-Type: application/json' -H 'Authorization: Bearer API_KEY' -d '{"prompt": "Hello, World!", "max_tokens": 5}' https://api.openai.com/v1/engines/davinci-codex/completions)aw";
std::system(command.c_str());
}
英文:
Don't use "
around strings that will be interpreted by the shell. Shells like [tag:bash] will interpret !
in Hello, World!
as an event. Instead, use '
around the strings that you send to the shell.
Also, use raw string literals (point 6 in that list) to not have to escape your special characters.
Example:
#include <cstdlib>
#include <iostream>
int main() {
std::string command =
R"aw(curl -X POST -H 'Content-Type: application/json' -H 'Authorization: Bearer API_KEY' -d '{"prompt": "Hello, World!", "max_tokens": 5}' https://api.openai.com/v1/engines/davinci-codex/completions)aw";
std::system(command.c_str());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论