I'm using the http package to make a POST request to OpenAI, and the API seems to only strictly accept application/sdp
as the Content-Type
:
flutter: Response body: {"error":{"message":"Unsupported content type. This API method only accepts 'application/sdp' requests, but you specified the header 'Content-Type: application/sdp; charset=utf-8'. Please try again with a supported content type.","type":"invalid_request_error","param":null,"code":"unsupported_content_type"}}
The Dart http
package, for some reason, seems to automatically add charset=utf-8
if charset is not set. The way to prevent this behaviour is to set the request body first before setting the headers:
// Create request manually to ensure headers are set after body
final request = Request('POST', Uri.parse('$baseUrl?model=$model'));
request.body = sdp;
request.headers['Authorization'] = 'Bearer $ephemeralKey';
request.headers['Content-Type'] = 'application/sdp';
final streamedResponse = await request.send();
final response = await Response.fromStream(streamedResponse);
The solution was buried deep in an issue comment, so hopefully this helps someone!