-2

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 support@openai.com and include any relevant code you’d like help with.)",
        "type": "invalid_request_error",
        "param": null,
        "code": null
    }
}
Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
  • 1
    You find yourself in escape hell. You have at least two layers of escape you need to deal with: First of all you need to escape within the C++ string, and then you also need to do a shell escape so that the individual `-H` parameters are correctly interpreted by the shell. This means that at the very least all quotation marks within the `-H` parameters (but **not** those delimiting the parameter itself) need to be `\\\"` instead of `\"`. However, doing this this way is a really bad idea, esp. when you start including external input in the strings. Please take a look at libcurl's API instead... – chris_se Mar 06 '23 at 19:29
  • 1
    Use raw string literals `R"(...)"`, it will improve the readability. – 273K Mar 06 '23 at 19:29
  • All these problems will go away if you stop spawning `curl.exe` as a child process. Link with libcurl and call it as an ordinary function. – Ben Voigt Mar 06 '23 at 20:02
  • @NazarHurin Did you get it working or do you need me to explain the answer in more detail? – Ted Lyngmo Mar 08 '23 at 18:12

1 Answers1

1

Don't use " around strings that will be interpreted by the shell. Shells like 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());
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108