how to send multipart/related POST request from browser in javascript?
Asked Answered
W

1

6

I would like to construct a POST as described in the following link

googleDriveMultipartUpload

I was using axios up until now but it doesn't support multipart/related type. Is there a work around or a more straight forward solution.

Walachia answered 19/5, 2018 at 14:22 Comment(0)
P
-1

This is not possible with axios because it doesn't allow multipart requests.

You can however use the Google Drive node library as explained here:

/**
 * Insert new file.
 * @return{obj} file Id
 * */
async function uploadBasic() {
  const fs = require('fs');
  const {GoogleAuth} = require('google-auth-library');
  const {google} = require('googleapis');

  // Get credentials and build service
  // TODO (developer) - Use appropriate auth mechanism for your app
  const auth = new GoogleAuth({
    scopes: 'https://www.googleapis.com/auth/drive',
  });
  const service = google.drive({version: 'v3', auth});
  const requestBody = {
    name: 'photo.jpg',
    fields: 'id',
  };
  const media = {
    mimeType: 'image/jpeg',
    body: fs.createReadStream('files/photo.jpg'),
  };
  try {
    const file = await service.files.create({
      requestBody,
      media: media,
    });
    console.log('File Id:', file.data.id);
    return file.data.id;
  } catch (err) {
    // TODO(developer) - Handle error
    throw err;
  }
}
Prudential answered 6/7, 2023 at 15:16 Comment(1)
Re edit: The question clearly states they are using a browser so the Node.js specific parts of that code won't work.Anam

© 2022 - 2024 — McMap. All rights reserved.