nativescript-background-http
nativescript-background-http copied to clipboard
Multipart upload file to google drive API
I am meeting with dificulties while using multipartUpload method. I need to send json file to the Google Drive API. I have to use multipart to set metadata to a file like name. Without multipart request file will be with untitled name. I don't know how to set boundaries according to Google Drive API documentations. Anyone can help?
This is my code:
let documents = knownFolders.documents();
let folder = documents.getFolder("testFolder");
let file = folder.getFile(("test") + ".json");
var xxx = { "thing": "cat", "age": "19" };
var jsonFile = JSON.stringify(xxx);
file.writeText(jsonFile)
.then(result => {
var bghttp = require("nativescript-background-http");
var session = bghttp.session("file-upload");
var request = {
url: "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
method: "POST",
headers: {
"Content-Type": "multipart/related",
"Authorization": `Bearer ${this.token.accessToken}`,
},
utf8: true
};
var params = [
{ name: "test", value: "I need to send metadata to the file I don't know how" },
{ name: "test", filename: file.path, mimeType: "application/json" }
];
var task = session.multipartUpload(params, request);
I have tried a lot but nothing worked out.
Google API https://developers.google.com/drive/api/v3/manage-uploads
Can you try saving your metadata as a json file and uploading it like you do the other file? This way you will be able to specify the content type of the metadata as application/json; charset=UTF-8
as the GDrive API expects.