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

标签 python python-3.x python-requests postman

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

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

但是,当我尝试通过 Python 发出请求时,我总是得到响应 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

Python 代码,由 Postman 制作:

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)

出于保密原因,已将 API 链接屏蔽为 {{BASE_URL}}

postman 回复:

{
    "id": "5e69058e2690d5b0e519cf4006dfdbfeeb5261b935094a2173b2e79a58e80ab5",
    "name": "aab2wieuqcnvn3g6syadumik4bsg5.0062.pdf",
    "fileIds": {
        "original": "5e69058e2690d5b0e519cf4006dfdbfeeb5261b935094a2173b2e79a58e80ab5.pdf"
    },
    "creationDate": "2019-06-20T09:41:59.5930472+00:00"
}

Python 的响应:

Response<500>

更新:

尝试了 GET 请求 - 工作正常,因为我收到了它的 JSON 响应。我想问题出在发布pdf文件中。关于如何将文件发布到 API 是否有任何其他选项?

postman 回复原始:

POST /api/v1/documents
Content-Type: multipart/form-data; boundary=--------------------------375732980407830821611925
cache-control: no-cache
Postman-Token: 3e63d5a1-12cf-4f6b-8f16-3d41534549b9
User-Agent: PostmanRuntime/7.6.0
Accept: */*
Host: {{BASE_URL}}
cookie: c2b8faabe4d7f930c0f28c73aa7cafa9=736a1712f7a3dab03dd48a80403dd4ea
accept-encoding: gzip, deflate
content-length: 3123756

file=[object Object]

HTTP/1.1 200
status: 200
Date: Thu, 20 Jun 2019 10:59:55 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Location: /api/v1/files/95463e88527ecdc94393fde685ab1d05fa0ee0b924942f445b14b75e983c927e
api-supported-versions: 1.0
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
Referrer-Policy: strict-origin

{"id":"95463e88527ecdc94393fde685ab1d05fa0ee0b924942f445b14b75e983c927e","name":"aab2wieuqcnvn3g6syadumik4bsg5.0062.pdf","fileIds":{"original":"95463e88527ecdc94393fde685ab1d05fa0ee0b924942f445b14b75e983c927e.pdf"},"creationDate":"2019-06-20T10:59:55.7038573+00:00"}

正确的请求

因此,最终 - 正确的代码如下:

import requests

files = {
    'file': open('/home/train/aab2wieuqcnvn3g6syadumik4bsg5.0062.pdf', 'rb'),
}
response = requests.post('{{BASE_URL}}/api/v1/documents', files=files, verify=False)
print (response.text)

最佳答案

A 500 error表示内部服务器错误,而不是您的脚本错误。

如果您收到 500 错误(与 400 error 不同,后者表示错误请求),那么理论上您的脚本没有问题,需要调整的是服务器端代码。

在实践中,它仍然可能是由于错误的请求。

如果您是运行 API 的人,那么您可以检查错误日志并逐行调试代码以找出服务器抛出错误的原因。

但在这种情况下,这听起来像是第三方 API,对吗?如果是这样,我建议您查看他们的文档以找到一个有效的示例,或者如果您认为这是他们的问题(这不太可能但有可能),请与他们联系。

关于python - 如何修复 python 请求中的 <Response 500> 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56683240/

相关文章:

python - 为什么在 Python 中这个是 "Invalid syntax"?

python - 收款计数器 - 如何从输出中删除 "counter"

python - Tiktok api/用户/信息端点

python - 使用 #!python2 无法在 Python 2 下运行

python - 尝试将多个 .csv 读取到单独的数据框列中

python - 请求流示例在我的环境中不起作用

python - 属性错误 : module 'requests' has no attribute 'Session' in pandas-DataReader

python - 有什么比 django-piston 更好的吗?

python - 比较单列中的值

python - 嵌套函数是 Pythonic 的吗?