简体   繁体   中英

passing binary data to rest-api

I want to upload a thumbnail to viemo api , and based on Vimeo doc, I must include the thumbnail file as binary data in the request body, I have to upload the thumbnail from the client side "fontend" and will send the request from the rest-api "backend" the problem is my rest-api can't receive the binary data, properly because I'm using express.js, is there any way to handle the binary data in the backend.

the steps as following:

client side sent thumbnail data => backend receive the data through the request body and send it to => endpoint

client side request

  const handleSubmit = (event) => {
    event.preventDefault();
    const formData = new FormData();
    formData.append('selectedFile', new Blob([selectedFile], { type: 'application/json' }));
    formData.append('uploadLink', uploadLink);

    const headers = {
      'Content-Type': 'application/json',
      Accept: 'application/vnd.vimeo.*+json;version=3.4',
    };

    try {
      axios
        .post(`${backendPostPath}/thumbnail-upload`, formData, {
          headers,
        })
        .then((response) => {
          applyThumbnial();
          console.log(`${uploadLink}link for upload`);
        });
    } catch (error) {
      console.log(error);
    }
  }; 

backend request can't receive the body request of frontend post as binary data,

const ThumbnailUpload = async (req, res) => {
  const { uploadLink } = req.body;
  const { selectedFile } = req.body;
  console.log(uploadLink);
  const clientServerOptions = {
    uri: `${uploadLink}`,
    encoding: null,
    body: JSON.stringify({
      name: uploadLink,
      file: selectedFile,
    }),
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
      Accept: 'application/vnd.vimeo.*+json;version=3.4',
      Authorization: getVimeoAuthorization(),
    },
  };
  request(clientServerOptions, function (error, response) {
    if (error) {
      res.send(error);
    } else {
      const body = JSON.parse(response.body);
      res.send(body);
    }
  });
};

is there any way to make the backend request body fetch the binary data, as I getting the data as "undefined"

Sorry for late update, I solved this issue by using the same put request form the client side, as the put request don't require Vimeo access token, so you can use the same put request i mentioned above, and remove authentication from the header, like following

const handleSubmit = (event) => {
    event.preventDefault();
    const formData = new FormData();
    formData.append('selectedFile', new Blob([selectedFile], { type: 'image/jpg, image/png' }));
    // formData.append('uploadLink', uploadLink);

  

    const headers = {
      'Content-Type': 'image/jpg, image/png',
      Accept: 'application/vnd.vimeo.*+json;version=3.4',
    };

    try {
      axios
        .put(`${uploadLink}`, formData, {
          headers,
        })
        .then((response) => {
      
          console.log(`${uploadLink}link for upload`);
        });
    } catch (error) {
      console.log(error);
    }
  }; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM