小编sai*_*off的帖子

如何修复 python 请求中的 <Response 500> 错误?

我正在使用一个 API,它接收一个 pdf 文件并进行一些分析,但我总是收到响应 500

使用 Postman 进行了初步测试,请求通过,收到响应 200 和相应的 JSON 信息。应关闭 SSL 安全性。

但是,当我尝试通过 Python 进行请求时,我总是得到 Response 500

我写的Python代码:

import requests

url = "https://{{BASE_URL}}/api/v1/documents"
fin = open('/home/train/aab2wieuqcnvn3g6syadumik4bsg5.0062.pdf', 'rb')
files = {'file': fin}
r = requests.post(url, files=files, verify=False)
print (r)
#r.text is empty
Run Code Online (Sandbox Code Playgroud)

邮递员生成的 Python 代码:

import requests

url = "https://{{BASE_URL}}/api/v1/documents"

payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"aab2wieuqcnvn3g6syadumik4bsg5.0062.pdf\"\r\nContent-Type: application/pdf\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
    'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    'Content-Type': "application/x-www-form-urlencoded",
    'cache-control': "no-cache",
    'Postman-Token': "65f888e2-c1e6-4108-ad76-f698aaf2b542"
    }

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)
Run Code Online (Sandbox Code Playgroud)

由于机密性,已将 API …

python python-3.x python-requests postman

8
推荐指数
1
解决办法
3万
查看次数

标签 统计

postman ×1

python ×1

python-3.x ×1

python-requests ×1