OpenAI transcription API bad request

题意:"OpenAI 转录 API 错误请求"

问题背景:

javascript 复制代码
const FormData = require('form-data');

    const data = new FormData();

    console.log('buffer: ', buffer);
    console.log('typeof buffer: ', typeof buffer);

    const filename = new Date().getTime().toString() + '.webm';
    data.append('model', 'whisper-1');
    data.append('file', bufferToStream(buffer));
    
    console.log('data: ', data);

    let config = {
        method: "post",
        maxBodyLength: Infinity,
        url: "https://api.openai.com/v1/audio/transcriptions",
        headers: {
        Authorization:
            `Bearer ${process.env.OPENAI_API_KEY}`,
            "Content-Type": "multipart/form-data",
            ...data.getHeaders(),
        },
        data: data,
    };

    await axios.request(config)

I am trying to post a file to openAI to get transcription. But I constantly get 400 bad request error.

"我正在尝试向 OpenAI 发送一个文件以获取转录。但是我不断收到 400 错误请求。"

I am using JavaScript (not TypeScript) so I can't use the FormData package, at least I have tried it and it didn't help me.

"我正在使用 JavaScript(而不是 TypeScript),所以我不能使用 FormData 包。至少我尝试过了,它没有帮助我。"

Error: Data after transformation must be a string, an Array Buffer, a Buffer, or a Stream

I tried Postman and it works, which provides me with the following code snippet:

"我尝试了 Postman,它有效,这里是它给我的代码片段:"

javascript 复制代码
const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');
let data = new FormData();
data.append('model', 'whisper-1');
data.append('file', fs.createReadStream('/Users/UserName/Downloads/session-1708252969167.webm'));

let config = {
  method: 'post',
  maxBodyLength: Infinity,
  url: 'https://api.openai.com/v1/audio/transcriptions',
  headers: { 
    'Authorization': {{OPENAI_API_KEY}}, 
    'Cookie': '__cf_bm=5L5OJyh1Yd28yUT9Uv0TT9F5POhL1fzeraSZF1C.9OA-1708337727-1.0-AV+XT0hftIn7mtJxICJduMQBpFcB9UQvbil6GmgI2BX7rw0BZRvuseeN0QgeuIt7KyNTxGJs/xcWIKMpkQdvzC4=; _cfuvid=3uLOzYX3DlLxSjGMLDqD0qcGIj.4zkEXt2czm9OiEfU-1708251423683-0.0-604800000', 
    ...data.getHeaders()
  },
  data : data
};

axios.request(config)
.then((response) => {
  console.log(JSON.stringify(response.data));
})
.catch((error) => {
  console.log(error);
});

I tried this code snippet as well, but it throw me a 400 error.

"我也尝试了这个代码片段,但它仍然返回了 400 错误。"

What did I do wrong?

"我哪里做错了?"

问题解决:

This code will work

"这段代码会有效。"

Save as trans.js

javascript 复制代码
const axios = require('axios');
const fs = require('fs');
const FormData = require('form-data');

// Function to make the API call
async function callOpenAI() {
  try {
    const fileStream = fs.createReadStream('audio.webm');

    const formData = new FormData();
    formData.append('file', fileStream);
    formData.append('model', 'whisper-1');

    const response = await axios.post('https://api.openai.com/v1/audio/transcriptions', formData, {
      headers: {
        'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`,
        ...formData.getHeaders() // Get headers manually
      }
    });

    console.log(response.data);
  } catch (error) {
    console.error('Error:', error.response.data);
  }
}

// Call the function
callOpenAI();

Check API by Postman

Set environment variable for API KEY

On Git Bash

bash 复制代码
OPENAI_API_KEY=[your API KEY]
Install dependencies 安装依赖
bash 复制代码
npm install axios fs form-data
Run it 运行
bash 复制代码
node trans.js
Result 结果
相关推荐
实在智能RPA2 小时前
金融行业财务审核自动化工具推荐:2026企业级AI Agent与智能合规选型指南
人工智能·ai·金融·自动化
Java小白中的菜鸟2 小时前
配置你的Claude模型
ai
神探小白牙2 小时前
eCharts 多系列柱状图增加背景图
javascript·ecmascript·echarts
Agent产品评测局2 小时前
生产排期与MES/ERP系统打通,实操方法详解 —— 2026企业级智能体自动化选型与实战指南
java·运维·人工智能·ai·chatgpt·自动化
Yoyo25年秋招冲冲冲3 小时前
【亲测可用】ubuntu系统下安装Openclaw+配置飞书
linux·ubuntu·ai·飞书·openclaw
ofoxcoding4 小时前
Claude Opus 4.7 API 怎么计费?2026 最新价格拆解与调用实战
ai
灵机一物4 小时前
灵机一物AI原生电商小程序、PC端(已上线)-【无标Anthropic 研究深度解析:AI 对就业市场的实际冲击与高危职业排行题】
人工智能·ai·程序员·职业发展·anthropic·就业市场
AI进化营-智能译站4 小时前
Jazzy ROS2入门指南系列05-配置VsCode实现ROS2项目开发
ide·vscode·ai·编辑器
一粒黑子5 小时前
【实测】GitNexus实测:拖入GitHub链接秒出代码知识图谱,今天涨了857星
人工智能·gpt·安全·ai·大模型·ai编程
笨蛋©5 小时前
2026质量管理数字化实战:基于Infra CONVERT与AI识别的检验计划(FAI/PPAP)高效方案
ai·数字化·cad·质量管理·制造业