Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python JSON API-发送PDF文件-类型错误:字节类型的对象不可JSON序列化_Python_Json_Rest_Pdf_Binary - Fatal编程技术网

Python JSON API-发送PDF文件-类型错误:字节类型的对象不可JSON序列化

Python JSON API-发送PDF文件-类型错误:字节类型的对象不可JSON序列化,python,json,rest,pdf,binary,Python,Json,Rest,Pdf,Binary,我正在尝试将pdf文件发送到 我可以发送txt文件,但发送pdf有问题 如果我使用value=base64.b64encodef.read.decode,它可以工作,但pdf文件无法打开,因此它已损坏 如果我不追加.decode,它会引发TypeError:bytes类型的对象不可JSON序列化 如果使用“r”模式而不是“rb”,则会引发TypeError:bytes类型的对象不可JSON序列化 你知道如何让它工作吗?你需要在base64编码的数据中使用附件\u二进制文件,而不是附件。我本来打算

我正在尝试将pdf文件发送到

我可以发送txt文件,但发送pdf有问题

如果我使用value=base64.b64encodef.read.decode,它可以工作,但pdf文件无法打开,因此它已损坏

如果我不追加.decode,它会引发TypeError:bytes类型的对象不可JSON序列化

如果使用“r”模式而不是“rb”,则会引发TypeError:bytes类型的对象不可JSON序列化


你知道如何让它工作吗?

你需要在base64编码的数据中使用附件\u二进制文件,而不是附件。

我本来打算这么做,但后来我忘了:谢谢
    import base64

    def fn():
        value = None
        with open('/tmp/test.pdf', 'rb') as f:
            value = base64.b64encode(f.read()).decode()


        payload = {"email": {
                    "subject": "Test",
                    "template": {
                        "id": tmplt_id,
                        "variables": context,
                    },
                    "from": {
                        "name": 'myname.sk',
                        "email": sender_email
                    }, "to": [{
                        "email": r,
                    } for r in recipients],
                    'attachments':
                        {
                           'test.pdf':value
                        }

                }
                }
        return requests.post('https://api.sendpulse.com/smtp/emails', headers=self.get_request_headers(),
                             json=payload)