David Choi
David Choi

Reputation: 111

How can I send parameters through the fetch api?

I'm currently trying to export a file from the Google Drive API. However, I don't know how to pass the mimeType parameter correctly using fetch. I tried the code below:

function getFile (access_token) {
    const fileUrl = "https://www.googleapis.com/drive/v3/files/" + imageID + "/export";
    let fileRequest = {
        method: "GET",
        mimeType: 'image/png',
        headers: new Headers({
            Authorization: "Bearer " + access_token
        })
    }

    fetch(fileUrl, fileRequest).then( response => {
        return(response.json());
    }).then( file =>  {
        console.log(file);
    });

The error I'm getting is:

Required parameter: mimeType

Upvotes: 0

Views: 74

Answers (1)

Rafa Guillermo
Rafa Guillermo

Reputation: 15377

In normal circumstances:

You should attach the headers using the Headers class:

function getFile(token, id) {
  const url = "https://www.googleapis.com/drive/v3/files/" + id + "/export"
  const headers = new Headers()
  headers.append('Authorization', `Bearer ${token}`)
  headers.append('Content-Type', 'image/png')

  let options = {
    method: "get",
    headers
  }

  fetch(url, options).then(response => {
    // do stuff with response
  })
}

However:

As per the Files: export documentation:

Exports a Google Doc to the requested MIME type and returns the exported content. Please note that the exported content is limited to 10MB.

So images are not supported by this endpoint. You will need to use Files: get instead. You can do this simply by removing the '/export from the end of the URL.

Upvotes: 1

Related Questions