【问题标题】:Python's Requests library adds tags to PDF, which breaks APIPython 的 Requests 库将标签添加到 PDF,这会破坏 API
【发布时间】:2017-05-01 13:18:09
【问题描述】:

我正在使用 Python 的 Requests 库将 PDF 发布到文档存储,然后将上传的 PDF 用于签名过程。但是,当使用 Python(而不是 CURL)上传 PDF 时,签名环境不起作用。在比较不同的文件时,我发现 Requests 向 PDF 添加了一些数据:

--ca9a0d04edf64b3395e62c72c7c143a5
Content-Disposition: form-data; name="LoI.pdf"; filename="LoI.pdf"

%%Original PDF goes here%%

--ca9a0d04edf64b3395e62c72c7c143a5--

这些数据可以被不同的 PDF 阅读器完全接受,但不能被 Signature API 接受。有没有办法阻止请求将此数据添加到 PDF 中?我使用了以下代码:

myfile = request.FILES['myfile']
url = %%documentstoreURL%%
resp = requests.request('post', url, files={myfile.name:myfile}, headers={'Content-Type':'application/pdf'}, auth=(%%auth details%%))

谢谢!

【问题讨论】:

  • 文件中没有添加任何内容。您看到的是用于传输的多部分 MIME 编码文件。 API 似乎不支持这种标准的编码方式。那么它还接受什么?
  • 你用的是什么 curl 命令,有效吗?
  • @KlausD。如果我从文档库下载 PDF,然后使用 Notepad++ 打开它,它仍然会显示这些“标签”。那么可能在接收端确实处理不好?
  • mwchase, curl -u %%auth_details%% -k --data-binary @mydocument.pdf -H "Content-Type: application/pdf" https://%%documentstoreURL%%

标签: python pdf python-requests


【解决方案1】:

您使用 curl 将文件作为二进制数据发送,但将其附加在请求中。

我阅读了源代码,我相信resp = requests.request('post', url, data={myfile.name:myfile}, headers={'Content-Type':'application/pdf'}, auth=(%%auth details%%))data 而不是files)将避免多部分编码。

至少,它应该被不同地破坏。

【讨论】:

  • 谢谢!这几乎解决了它。它让我走上了正确的轨道:)。我发布了最终的工作解决方案。
【解决方案2】:

在正确的方向指导下,我找到了基于Python requests - POST data from a file的可行解决方案

最后我是这样做的:

 myfile = request.FILES['myfile']
 payload = request.FILES['myfile'].read()
 headers = {'content-type': 'application/pdf'}
 url = "%%DocumentServiceURL"
 r = requests.post(url, auth=(%%auth_details%%), data=payload, verify=False, headers=headers)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 2021-10-30
    相关资源
    最近更新 更多