I am using Postman to send and receive API requests from OpenAI’s ChatGPT API. I’m having an issue with formatting the prompt I want to use.
I’ve been able to get the API working on Postman, but I’m having trouble sending arrays. This is the format of the body for the API request:
{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "Whatever question I want to ask ChatGPT"
}
]
}
Sending a request like this works fine. However, I want to put an associative array in the “content” key.
Here is an example of an array I want to send:
$array = [
'songName' => "RandomSong",
'lyrics' => "lyrics line one / lyrics line two"
];
When I echo this code in PHP with echo json_encode($array)
and copy/paste that result into Postman, I get this:
{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "{"songName":"RandomSong","lyrics":"lyrics line one / lyrics line two"}"
}
]
}
When I make this API request, I get an error message:
{
"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
}
}
It seems that the double quotation might be causing an issue here. Any ideas on how I can potentially fix this issue?
Notes
- I have also tried wrapping the key and value pairs with
json_encode
to no avail.