Python请求POST到java REST接口MultipartFile参数不存在
我在这里搜索过,也在网上搜索过其他地方,似乎找不到答案,我认为这是我的一个简单错误。基本上,我希望通过发出Python请求将文件从一台机器传输到另一台机器。将请求发布到远程计算机上的Java REST接口。Java方面如下所示:
@ApiOperation(value = "Binary file transfer", nickname = "Binary file transfer")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Success", response = HttpMessageInformationReturnDataBean.class),
@ApiResponse(code = 404, message = "Not Found")})
@RequestMapping(value = "/vm/{version}/uploadbinfile", method = RequestMethod.POST)
public String handleFileUpload(@RequestParam("binaryFile") MultipartFile file) {
if (!file.isEmpty())
{
try
{ ... the code that handles the transfer
在Python方面,该方法如下所示:
def xfer_trm_binaries(self):
params = {"file": ('binaryFile',os.path.basename('TRMServer.jar')),
"folder": os.path.dirname(self.dest_temp_path),
"submit": "Submit"}
url = self.form_url("/vm/v1/uploadbinfile", self.trm_server_ip_address, self.vrm_server_port)
header=self.form_header(self.vrm_key)
header['Content-Type'] = 'multipart/file-data; boundary=randomboundarysequence'
header['enctype'] = "multipart/file-data"
print 'Send :' + url
binfile = self.local_jar_path+'TRMServer.jar'
with open(binfile, 'rb') as mfile:
try:
result = requests.post(url, headers=header,
data=params, files={'file': mfile}, verify=False)
except Exception:
在那里组装的标头如下所示:
{'Content-Type': 'multipart/file-data; boundary=randomboundarysequence', 'Accept': 'application/json', 'Authorization': u'Bearer 8b2b6e53-9008-44b7-9d34-b5ecb9659250', 'enctype': 'multipart/file-data'}
请求已发送,但响应始终为400错误,因为它抱怨缺少MultipartFile参数“binaryFile”:
'{"timestamp":1488597880207,"status":400,"error":"Bad Request","exception":"org.springframework.web.bind.MissingServletRequestParameterException","message":"Required MultipartFile parameter \\'binaryFile\\' is not present","path":"/vm/v1/uploadbinfile"}'
我曾尝试向请求的参数和头添加一个“name”值,但它总是返回400代码。有人知道我可能做错了什么吗
# 1 楼答案
事实上,我最终找到了答案——基本上我有一个方法,它形成了一个包含oauth承载令牌以及ContentType和AcceptType的头部。。。然后我用多部分文件信息重写了这些文件。这就是接收REST接口所不喜欢的。当我刚刚完全消除了这些标题属性时,它似乎自己就明白了