-2

我正在尝试使用 fetch 向 Cloud Convert 发布 API 调用并收到以下错误:

消息:“给定的数据无效。” 代码:“INVALID_DATA”错误:{...} 任务:数组(1)0:“任务字段是必需的。”

这是我的代码(在 Wix 上):

export async function convertMp4toMp3(fileUrl, filename) {
    filename = filename.replace(/mp4/gi, "mp3")
    let job = {
        tasks: {
            "import-2": {
                "operation": "import/url",
                "url": fileUrl
            },
            "task-1": {
                "operation": "convert",
                "input_format": "mp4",
                "output_format": "mp3",
                "engine": "ffmpeg",
                "input": [
                    "import-2"
                ],
                "audio_codec": "mp3",
                "audio_qscale": 0
            },
            "export-1": {
                "operation": "export/google-cloud-storage",
                "input": [
                    "task-1"
                ],
                "project_id": "project-id",
                "bucket": "bucket",
                "client_email": "client_emailXXXXXXX",
                "file": filename,
                "private_key": "My Private Key
            }
        }
    }
    let options = {
        "method": "POST",
        "body": job,
        "headers": {
            "Authorization": "Bearer MyApiKey",
            "Content-type": "application/json"
        }
    }
    let response = await fetch("https://api.cloudconvert.com/v2/jobs", options)
    console.log(response.json())
}

如您所见,“任务”字段填充了作业......

4

1 回答 1

1

fetchAPI 不会自动对 JSON 进行编码。尝试:

  let options = {
        "method": "POST",
        "body": JSON.stringify(job),
        "headers": {
            "Authorization": "Bearer MyApiKey",
            "Content-type": "application/json"
        }
    }
于 2020-01-19T18:06:50.137 回答