0

I am trying to upload some videos to Cloudflare Stream API. Here is official documentation and its example request using curl: https://developers.cloudflare.com/stream/uploading-videos/upload-video-file/

I am doing the request in Node.js

const uploadVideo = (video: Express.Multer.File): => {
    const formData = new URLSearchParams();
    formData.append('file', video);

    let cloudflareResponse;

    try {
        cloudflareResponse = await axios.post(
            `https://api.cloudflare.com/client/v4/accounts/${ACCOUNT_ID}/stream/copy`,
            formData,
            {
                headers: {
                    Authorization: `Bearer ${API_KEY}`,
                    'Content-Type': 'multipart/form-data',
                    //'Tus-Resumable': '1.0.0',
                    //'Upload-Length': '600',
                    //'Upload-Metadata': 'maxDurationSeconds 600'
                }
            }
        );
    } catch (e) {
        console.log('Error while trying to upload video to Cloudflare API ', e);
    }
}

The commented Headers I took from this article, in which the request is done in Django and I tried to replicate it https://medium.com/@berman82312/how-to-setup-cloudflare-stream-direct-creator-uploads-correctly-802c37cbfd0e

The error I am getting is a 400 and here is some of the response

  config: {
    url: 'https://api.cloudflare.com/client/v4/accounts/...../stream/copy',
    method: 'post',
    data: 'file=%5Bobject+Object%5D',
    headers: {
      Accept: 'application/json, text/plain, */*',
      'Content-Type': 'multipart/form-data',
      Authorization: 'Bearer .....',
      'Tus-Resumable': '1.0.0',
      'Upload-Length': '600',
      'Upload-Metadata': 'maxDurationSeconds 600',
      'User-Agent': 'axios/0.21.1',
      'Content-Length': 24
    },
  data: { result: null, success: false, errors: [Array], messages: null }

I am sure something is wrong in the request and hope someone could help me spot the mistake or suggest some modifications that might help. I have been stuck with this problem for hours and on Postman I am also getting a 400 response when trying to send with form-data.

user12051965
  • 97
  • 9
  • 29
  • Are you sure `formData` should be an instance of `URLSearchParams` and not `FormData` ? – Seblor Apr 30 '22 at 23:35
  • @Seblor I used ```FormData``` first but I got a ```FormData is not defined``` error and saw the following thead so I used ```URLSearchParams``` instead https://stackoverflow.com/questions/63576988/how-to-use-formdata-in-node-js-without-browser – user12051965 May 01 '22 at 09:26

0 Answers0