-2

I am trying to use fetch to POST an API call to Cloud Convert and getting the following error:

message: "The given data was invalid." code: "INVALID_DATA" errors: {...} tasks: Array(1) 0: "The tasks field is required."

Here is my code (on 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())
}

As you can see, the "tasks" field is populated with the jobs...

Mennyg
  • 330
  • 2
  • 11

1 Answers1

1

The fetch API does not automatically encode JSON. Try:

  let options = {
        "method": "POST",
        "body": JSON.stringify(job),
        "headers": {
            "Authorization": "Bearer MyApiKey",
            "Content-type": "application/json"
        }
    }
monday
  • 287
  • 2
  • 3