You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Node.js Request调用Acast API时遇音频格式错误求助

音频上传错误(400 "audio file wrong format")排查方案

以下是针对你遇到的Acast API上传音频报错问题的具体排查步骤:

1. 修复代码语法错误

你的代码中audio.options部分存在语法错误,filename后缺少逗号,这会导致formData构造异常,服务器无法正确解析音频数据:

// 错误写法
'options': {
  'filename': 'filename'
  'contentType': null
}

// 正确写法
'options': {
  'filename': 'filename',
  'contentType': null
}

2. 验证文件完整性与路径

  • 确认/path/to/file是正确的文件路径:可以通过fs.existsSync('/path/to/file')检查文件是否存在,避免读取不存在的文件流。
  • 验证文件本身无损坏:用本地音频播放器打开文件,或使用ffmpeg -i your-file.mp3命令检查文件格式是否正常,确保文件不是损坏的或伪MP3/WAV格式。

3. 明确设置音频Content-Type

不要将contentType设为null,明确指定对应格式的MIME类型,帮助服务器识别文件:

  • MP3文件:audio/mpeg
  • WAV文件:audio/wav

示例修改:

'audio': {
  'value': fs.createReadStream('/path/to/your-episode.mp3'),
  'options': {
    'filename': 'your-episode.mp3',
    'contentType': 'audio/mpeg'
  }
}

4. 检查API权限与端点正确性

  • 确认节目ID 5bcb76ef0d46b9226faefe92 是有效的,且你的x-api-key拥有该节目发布剧集的权限。可以先调用GET请求https://open.acast.com/rest/shows/5bcb76ef0d46b9226faefe92验证密钥和节目ID是否正常返回数据。

5. 替换已废弃的request库

request库已停止维护,存在兼容性风险,建议改用axios结合form-data库实现上传,确保请求格式正确:

const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');

const form = new FormData();
form.append('title', 'Episode Title');
form.append('subtitle', 'Episode Subtitle');
form.append('audio', fs.createReadStream('/path/to/your-episode.mp3'), {
  filename: 'your-episode.mp3',
  contentType: 'audio/mpeg'
});
form.append('status', 'published');

axios.post('https://open.acast.com/rest/shows/5bcb76ef0d46b9226faefe92/episodes', form, {
  headers: {
    ...form.getHeaders(),
    'x-api-key': 'secret'
  }
})
.then(res => console.log(res.data))
.catch(err => console.error(err.response?.data || err.message));

内容的提问来源于stack exchange,提问作者J88L33

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 06:15:29