Python中的补丁请求问题
我编写了一个应用程序,它从我使用的数据库服务下载一个文件,然后我将该文件转换为另一种格式并重新上传。问题在于上传。我正在使用补丁请求,它完成时没有错误,但该文件从未实际上载 这是我的密码:Python中的补丁请求问题,python,python-requests,airtable,Python,Python Requests,Airtable,我编写了一个应用程序,它从我使用的数据库服务下载一个文件,然后我将该文件转换为另一种格式并重新上传。问题在于上传。我正在使用补丁请求,它完成时没有错误,但该文件从未实际上载 这是我的密码: for person in r['records']: try: # Get Voicemail and handle conversion if necessary vm = person['fields']['Voicemail'][0]['url']
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))
…以及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"}
我对补丁请求不太熟悉,因此非常感谢您的帮助。厚颜无耻地复制另一个答案(我标记了问题)。这就是使用请求上传数据的方式。无论您是否使用GET、POST、PATCH或其他方式: 如果
upload\u文件
是该文件,请使用:
而请求
将发送一个多部分表单帖子正文,其中upload\u file字段设置为file.txt
文件的内容
原来的so帖子是可能的重复,我不认为这是重复的。请求成功完成,但未按预期上载。我认为这与本地url有关。如果我使用一个标准的非本地url,它就可以正常工作。请求文件包应该允许本地文件随请求一起上载。您必须在requests.path()命令中指定“files”参数。有关详细信息,请参阅我的答案。仅上载本地URL是不够的。文件的URL必须根据airtable api的文档在json参数中指定。我从请求中得到了200条回复,但实际上并没有上传文件。很抱歉,这对我的特殊情况没有帮助。仍然在寻找这个问题的解决方案。
files = {'upload_file': open('file.txt','rb')}
values = {'DB': 'photcat', 'OUT': 'csv', 'SHORT': 'short'}
r = requests.post(url, files=files, data=values)