Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify request body in Google API calls (using Google APIs Client Library for JavaScript)

I am trying to call Google API method drive.files.insert to create a folder in Google Drive with a request like this (using Google APIs Client Library for JavaScript):

var request = gapi.client.drive.files.insert({'convert': 'false', 'ocr': 'false'});
request.execute(function(resp) { console.log(resp); });

The problem is that I need to specify some params in the request body, for example:

{
    "title":"testFolder",
    "description":"hello world",
    "mimeType":"application/vnd.google-apps.folder"
}

But I cannot figure it out how to specify these parameters with the Google APIs Client Library for JavaScript. Is there any suggestion of how I can achieve this?

like image 964
KiL Avatar asked Aug 06 '12 06:08

KiL


People also ask

How do you handle request in API?

To make an API request, you can either make a direct HTTP request, by using tools like curl or httplib2 , or you can use one of the available client libraries. The image URI has a different project ID ( debian-cloud ) from your project ID because images belong to different projects, depending on the type of image.

What is gapi in JavaScript?

GAPI is Google's client library for browser-side JavaScript. It's used in Google Sign-in, Google Drive, and thousands of internal and external web pages for easily connecting with Google APIs.


2 Answers

Not necessarily gapi.client.request with body field.

You may try gapi.client.drive.files.insert({'convert': 'false', 'ocr': 'false','resource': resource}) where resource is actually what you want to send, e.g.

resource = {
    "title":"testFolder",
    "description":"hello world",
    "mimeType":"application/vnd.google-apps.folder"
}

I have not verified that but I have tried exactly the same scenario with sending request body for creating Google Task list (gapi.client.tasks.tasklists.insert)

like image 180
AlexG Avatar answered Oct 28 '22 04:10

AlexG


Use the "resource" keyword to send the body.

like image 30
Lioness Avatar answered Oct 28 '22 03:10

Lioness