Using Mathematica 13.0 on Windows 10
codes are below
req=HTTPRequest["https://api.openai.com/v1/completions",
<|Method->"POST",
"Body"->{"model"->"text-davinci-003","prompt"->"Say this is a test"},
"Headers"->{"Authorization"->"Bearer sk-xxxxxxxxxxxxx"}|>]
URLExecute[req]
and here is the output
{error->{message->you must provide a model parameter,type->invalid_request_error,param->Null,code->Null}}
It seems I set the wrong body parameters especially in "model"
part.
First I checked the openai Ducuments to set proper body parameters.Then I used postman to verify my body parameters and it works. However, when I try to reproduce POST request in Mathematica, the output returned by the site shows that I did not set body parameters correctly.
I also checked my request body by using
req["Body"]
and it returns normally
{"model": "text-davinci-003","prompt": "Say this is a test"}
I think the possible error is in setting the wrong format or position of the parameter. Could anyone give me some suggestions or hints on setting body parameter?
"Body"->ExportString[{"model"->"text-davinci-003","prompt"->"Say this is a test"},"JSON"]
$\endgroup$<| Method -> "POST", "Body" -> ExportString[{"model" -> "text-davinci-003", "prompt" -> "Say this is a test"}, "JSON"], "ContentType" -> "application/json",
$\endgroup$ContentType
parameter to post request. $\endgroup$