0

i need to upload video to bunny stream :bunny docs

i convert file to base64 as thy do here: upload file: BODY PARAMS

if you select js axios as LANGUAGE you'll see the data value set in base64

and this is my code:

function UploadVideo(e){
  const data = new FormData();
  let file = e.target.files[0];
  let video;
  const toBase64 = file => new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = error => reject(error);
  });
  
  async function Main() {
    video = await toBase64(file);
  }
  
  Main();


  
  const c_options = {
    method: 'POST',
    url: 'https://video.bunnycdn.com/library/49034/videos',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/*+json',
      AccessKey: ''
    },
    data: '{"title":"test"}'
  };
  
  axios.request(c_options).then(function (c_response) {
    //upload start
    const u_options = {
      method: 'PUT',
      url: `https://video.bunnycdn.com/library/49034/videos/${c_response.data.guid}`,
      headers: {
        Accept: 'application/json',
        AccessKey: ''
      },
      data: video,
    };
    axios.request(u_options).then(function (u_response) {
      //post url to php
      console.log(u_response.data);
    }).catch(function (error) {
      console.error(error);
    });
    //upload end

    console.log(c_response.data);
  }).catch(function (error) {
    console.error(error);
  });
}

but it return status code 400

The 400 error text: "Failed to read the request form. Form key length limit 2048 exceeded."

how can i do that?

Matt Ellen
  • 11,268
  • 4
  • 68
  • 90

1 Answers1

0

The error is telling that a key is too long.

Your data property is meant to be an object, because axios default Content-Type (which you have omitted) is application/x-www-form-urlencoded.

If you want to send the file, then you need to set the Content-Type in you header to Content-Type: application/octet-stream, i.e. your header object should be

headers: {
  Accept: 'application/json',
  AccessKey: '',
  Content-Type: 'application/octet-stream'
},

This shown in the javascript example on the bunny.net page you link to.

Matt Ellen
  • 11,268
  • 4
  • 68
  • 90