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:
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 [email protected] and include any relevant code you’d like help with.)",
"type": "invalid_request_error",
"param": null,
"code": null
}
}
Don't use
"
around strings that will be interpreted by the shell. Shells like bash will interpret!
inHello, 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: