0

I have an application I wrote that downloads a file from a database service I use and then I convert the file to another format and re-upload. The issue is with the upload. I am using a patch request and it completes without errors but the file is never actually uploaded.

Here is my code:

    for person in r['records']:
    try:
        # Get Voicemail and handle conversion if necessary
        vm = person['fields']['Voicemail'][0]['url']
        if '.m4a' in vm:
            vm_name = person['fields']['Voicemail'][0]['filename'].replace('.m4a', '').replace(' ', '')

            # Download file to local machine and convert to .mp3
            r = requests.get(vm, allow_redirects=True)
            open('{}.m4a'.format(vm_name), 'wb').write(r.content)
            bash = 'ffmpeg {0}.mp3 -i {0}.m4a -codec:a libmp3lame -qscale:a 1'.format(vm_name)
            os.system(bash)

            s = requests.Session()
            s.mount('file://', FileAdapter())
            cwd = os.getcwd()
            # url = s.get('file:///{}/{}.mp3'.format(cwd, vm_name))

            # Upload/delete files to server
            r = requests.patch('https://api.airtable.com/v0/{}/People/{}'.format(base_id, person['id']), 
                        json={"fields": {"Voicemail": [{"url": 'file:///{}/{}.mp3'.format(cwd, vm_name)}]}}, 
                        headers={"Authorization": "Bearer {}".format(at_auth), "Content-type": "application/Json"})

            print(r.text)

            # Delete temporary local files
            os.remove('{}.mp3'.format(vm_name))
            os.remove('{}.m4a'.format(vm_name))

...And the response of r.text:

{"id":"recnlJBNEWFMLwYNh","fields":{"Name":"Matthew Sewell","Phone":["reciInRjmNpyTabUS"],"Voicemail":[{"id":"att7YiG4s0Epa3V6o","url":"file:////Users/test/Dropbox/python/projects/business/testing123.mp3","filename":"testing123.mp3"}]},"createdTime":"2018-08-09T00:59:35.000Z"}

I am not super familiar with patch requests so any help is appreciated.

freefly0313
  • 105
  • 4
  • 14
  • Possible duplicate of [python requests file upload](https://stackoverflow.com/questions/22567306/python-requests-file-upload) – juliusmh Aug 09 '18 at 01:59
  • I don't think this is a duplicate. The requests completes successfully but does not upload as expected. I think it has something to do with the local url. If I use a standard, non-local url it works fine. The requests-file package is supposed to allow local file uploads with requests. – freefly0313 Aug 09 '18 at 21:00
  • you have to specify the "files" argument in the requests.path() command. See my answer for details. It does not suffice to upload just the local URL. – juliusmh Aug 10 '18 at 06:05
  • The url to the file must be specified in the json argument per documentation of the airtable api. I get a 200 response from the request, it just doesn't actually upload the file. – freefly0313 Aug 10 '18 at 22:01

1 Answers1

0

Shamelessly copy pasted from another answer (i flagged the question). This is how you can upload data using requests. It doesnt matter if you use GET, POST, PATCH or whatever:

If upload_file is meant to be the file, use:

files = {'upload_file': open('file.txt','rb')}
values = {'DB': 'photcat', 'OUT': 'csv', 'SHORT': 'short'}

r = requests.post(url, files=files, data=values)

and requests will send a multi-part form POST body with the upload_file field set to the contents of the file.txt file.

The original so post is here

juliusmh
  • 457
  • 3
  • 12