I am trying to use python requests library to send a file to Google Drive api. The only thing I need it to send a multipart request according to google documentation https://developers.google.com/drive/web/manage-uploads#multipart I need to send the meta data first and then the file. Here is what I tried so far
def upload_csv(self, file, description):
self.refresh()
url = self.url+'?uploadType=multipart&' + urllib.urlencode({'key':self.api_key})
headers = { 'Authorization':'Bearer {}'.format(self.access_token),
# 'content-type':'multipart/related',
# 'content-length':size
}
data = {'title':file,'description':description }
files = {'file':(file,open(file,'rb'),'text/csv')}
response = requests.post( url, headers = headers, data = data, files = files )
but I get back an error: u'Bad content type. Please use multipart. Is there a way to send meta data and the file using requests
I figured out a way to do it. The problem is Google wants the meta data and the file together.
def upload_csv(self, file, description):
self.refresh()
url = self.url+'?uploadType=multipart&convert=true' + urllib.urlencode({'key':self.api_key})
headers = { 'Authorization':'Bearer {}'.format(self.access_token) }
class DataDict(dict):
def read(self):
return str( self )
data = ('metadata',DataDict(title = file,description = description),'application/json; charset=UTF-8')
file = (file,open(file,'rb'),'text/csv')
files = {'data':data, 'file':file }
response = requests.post( url, headers = headers, files = files )
return respone
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With