繁体   English   中英

为什么通过python请求调用rest api时出现500 Internal Server Error?

[英]Why am I getting 500 Internal Server Error when calling post rest api via python request?

我正在尝试使用python请求库调用post rest API。 当我要求使用邮递员时,我能够得到正确的答复。 当我尝试使用python请求库调用它时,出现内部服务器错误。

import requests

url = "http://192.188.9.146:9886/getcontext/"

payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"project\"\r\n\r\ndaynight\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"D:\\Downloads\\GTA.Imaging.Services\\GTA.Imaging.Services.Wrapper.TestApp\\PatternMatchingdata\\Go_To_Setting_Screen.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
    'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    'cache-control': "no-cache",
    'Postman-Token': "79668e4b-305b-404e-904f-92fc71a12f9f"
    }

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

print(response.text)

这给我错误如下

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request.  Either the server is overloaded or there is an error in the application.</p>

当我使用postman客户端应用程序时,我得到输出与预期的响应Postman客户端我做错了什么? 任何指导将非常有帮助,谢谢。

删除payload参数并添加files 请参阅请求文档 ,该文档解释了request.request函数的这个选项。

import requests

url = "http://192.188.9.146:9886/getcontext/"

files = {'file': open('PATH_TO_FILE\Go_To_Setting_Screen.jpg','rb')}
data  = {'project': 'daynight'}

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

print(response.text)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM