我在这里以及其他地方在网上搜索过,似乎找不到我认为是一个简单错误的答案。基本上我想通过向远程机器上的Java REST接口发出Python requests.POST请求,将文件从一台机器传输到另一台机器。 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"}'
我试过添加一个'名称'值对请求的参数和标题都有效,但总是会返回400代码。有没有人知道我可能做错了什么?
答案 0 :(得分:0)
实际上我最终想出来了 - 基本上我有一个方法形成了包含oauth bearer标记的标题,以及ContentType和AcceptType ...然后我用多部分文件信息覆盖了那些。这就是接收REST接口不喜欢的东西。当我完全消除这些标题属性时,它似乎自己解决了这个问题。