繁体   English   中英

Python请求POST到Java REST接口的MultipartFile参数不存在

[英]Python requests POST to java REST interface MultipartFile parameter is not present

我在这里以及在线其他地方进行了搜索,但似乎找不到我认为是简单错误的答案。 基本上,我想通过向远程计算机上的Java REST接口发出Python request.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代码一起返回。 外面有人知道我在做什么错吗?

实际上,我最终弄清楚了这一点-基本上,我有一个方法形成了包含oauth承载令牌以及ContentType和AcceptType的标头,然后用多部分文件信息覆盖了它们。 那就是接收REST接口所不喜欢的。 当我完全消除了这些标头属性时,它似乎可以自行解决。

暂无
暂无

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

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