Search code examples
openai-apichatgpt-api

Using curl to request Chatgpt API on Windows Cmd


This is my command using Windonws Cmd:

curl -x 127.0.0.1:33210 https://api.openai.com/v1/chat/completions^
  -H "Content-Type: application/json" ^
  -H "Authorization: Bearer %OPENAI_API_KEY%" ^
  -d '{ ^
     "model": "gpt-3.5-turbo", ^
     "messages": [{"role": "user", "content": "Say this is a test!"}], ^
     "temperature": 0.7 ^
   }'

And Cmd returned the error reply below:

{
    "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 contact us through our help center at help.openai.com.)",
        "type": "invalid_request_error",
        "param": null,
        "code": null
    }
}

I can't find the error on the JSON body.I used the examples from Openai's documention.

After solving the format problem,the command is as below:

curl  -x 127.0.0.1:33210 https://api.openai.com/v1/chat/completions^
-H "Content-Type: application/json" ^
-H "Authorization: Bearer %OPENAI_API_KEY%" ^
-d "{"model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "Say this is a test!"}], "temperature": 0.7 }"

but a problem remains:

{
    "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 contact us through our help center at help.openai.com.)",
        "type": "invalid_request_error",
        "param": null,
        "code": null
    }
}
curl: (3) URL using bad/illegal format or missing URL
curl: (3) bad range specification in URL position 12:
messages: [{role: user, content: Say

It seems that the curl doesn't package all the sentance which I wish to package, I don't understand why.


Solution

  • This is caused by the ^ characters you parse in your JSON. The JSON data is parsed as a string, so we don't need the newline characters. A valid JSON command would be

    curl -x 127.0.0.1:33210 https://api.openai.com/v1/chat/completions^
      -H "Content-Type: application/json" ^
      -H "Authorization: Bearer %OPENAI_API_KEY%" ^
      -d '{"model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "Say this is a test!"}], "temperature": 0.7 }'
    

    EDIT

    The format problem is caused by using double quotes (") instead of single quotes (') around the JSON body. In this link, the following can be found in the first paragraph:

    JSON data is passed as a string. Double quotes in JSON must be escaped with the backslash "\ " on Windows computers.

    In your case, that means that the data parsed after the data switch -d should be formatted as follows:

    -d '{"model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "Say this is a test!"}], "temperature": 0.7 }'
    

    Note the single quotes around the whole JSON body, but the double quotes around the properties and values!