如何修复<响应 500>python请求中的错误?
问题描述
我正在使用一个 API,它接收一个 pdf 文件并进行一些分析,但我总是收到 Response 500
I am using an API, which receives a pdf file and does some analysis, but I am receiving Response 500 always
已使用 Postman 进行了初步测试,请求通过,收到带有相应 JSON 信息的响应 200.应关闭 SSL 安全性.
Have initially tested using Postman and the request goes through, receiving response 200 with the corresponding JSON information. The SSL security should be turned off.
但是,当我尝试通过 Python 发出请求时,我总是得到 Response 500
However, when I try to do request via Python, I always get Response 500
我写的Python代码:
Python code written by me:
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 生成:
Python code, produced by the Postman:
import requests
url = "https://{{BASE_URL}}/api/v1/documents"
payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="aab2wieuqcnvn3g6syadumik4bsg5.0062.pdf"
Content-Type: application/pdf
------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}}
Have masked the API link as {{BASE_URL}} due to the confidentiality
邮递员的回应:
{
"id": "5e69058e2690d5b0e519cf4006dfdbfeeb5261b935094a2173b2e79a58e80ab5",
"name": "aab2wieuqcnvn3g6syadumik4bsg5.0062.pdf",
"fileIds": {
"original": "5e69058e2690d5b0e519cf4006dfdbfeeb5261b935094a2173b2e79a58e80ab5.pdf"
},
"creationDate": "2019-06-20T09:41:59.5930472+00:00"
}
Python 的响应:
Response by Python:
Response<500>
更新:
尝试了 GET 请求 - 工作正常,因为我收到了 JSON 响应.我想问题在于发布pdf文件.关于如何将文件发布到 API,还有其他选项吗?
Tried the GET request - works fine, as I receive the JSON response from it. I guess the problem is in posting pdf file. Is there any other options on how to post a file to an API?
邮递员回应 RAW:
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"}
正确的请求
所以,最终 - 正确的代码如下:
So, eventually - the correct code is the following:
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 错误 表示内部服务器错误,而不是脚本错误.
A 500 error indicates an internal server error, not an error with your script.
如果您收到 500 错误(而不是 400错误,表示请求错误),那么理论上你的脚本没问题,需要调整的是服务器端代码.
If you're receiving a 500 error (as opposed to a 400 error, which indicates a bad request), then theoretically your script is fine and it's the server-side code that needs to be adjusted.
在实践中,它仍然可能是由于一个错误的请求.
In practice, it could still be due a bad request though.
如果您是运行 API 的人,那么您可以检查错误日志并逐行调试代码以找出服务器抛出错误的原因.
If you're the one running the API, then you can check the error logs and debug the code line-by-line to figure out why the server is throwing an error.
不过,在这种情况下,它听起来像是第三方 API,对吗?如果是这样,我建议您查看他们的文档以找到一个可行的示例,或者如果您认为这是他们的问题(这不太可能但可能),请与他们联系.
In this case though, it sounds like it's a third-party API, correct? If so, I recommend looking through their documentation to find a working example or contacting them if you think it's an issue on their end (which is unlikely but possible).
相关文章