Python通过API上传PDF

Python通过API上传PDF,python,Python,我正在尝试将.pdf文件发布到API。通过以下代码: from espo_api_client import EspoAPI import base64 binary_file = open('data/GetReport.pdf', 'rb') binary_file_data = binary_file.read() base64_encoded_data = base64.b64encode(binary_file_data) dataPush = { "name&

我正在尝试将.pdf文件发布到API。通过以下代码:

from espo_api_client import EspoAPI
import base64

binary_file = open('data/GetReport.pdf', 'rb')
binary_file_data = binary_file.read()
base64_encoded_data = base64.b64encode(binary_file_data)


dataPush = {
    "name": "GetReport.pdf",
    "type": 'application/pdf',
    "role": "Attachment",
    "relatedType": "EventIndex",
    "field": "document", 
    "file": (f'data:application/pdf;base64,{base}'),
}

push = client.request('POST', 'Attachment', dataPush)

pdf文件已成功上载,但当我尝试在服务器上查看pdf时,该文件为空。有人知道我做错了什么吗

你需要在发送数据时对其进行解码,这个片段应该可以工作

from espo_api_client import EspoAPI
import base64

binary_file = open('data/GetReport.pdf', 'rb')
binary_file_data = binary_file.read()
base64_encoded_data = base64.b64encode(binary_file_data)


dataPush = {
    "name": "GetReport.pdf",
    "type": 'application/pdf',
    "role": "Attachment",
    "relatedType": "EventIndex",
    "field": "document", 
    "file": (f'data:application/pdf;base64,{base64_encoded_data.decode()}'),
}

push = client.request('POST', 'Attachment', dataPush)

你用的是什么模块?请将相关的
导入
(s)添加到您的代码中。@Daweo我更新了我的帖子。谢谢!工作起来很有魅力。