简体   繁体   English

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

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

I've searched around here as well as elsewhere online and can't seem to find the answer for what I think is a simple error on my part. 我在这里以及在线其他地方进行了搜索,但似乎找不到我认为是简单错误的答案。 Basically I want to transfer a file from one machine to another by issuing a Python requests.POST request to a Java REST interface on the remote machine. 基本上,我想通过向远程计算机上的Java REST接口发出Python request.POST请求,将文件从一台计算机传输到另一台计算机。 The Java side looks like this: 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

On the Python side, the method looks like this: 在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:

The header that gets assembled there looks like this: 在那里组装的标题看起来像这样:

{'Content-Type': 'multipart/file-data; boundary=randomboundarysequence', 'Accept': 'application/json', 'Authorization': u'Bearer 8b2b6e53-9008-44b7-9d34-b5ecb9659250', 'enctype': 'multipart/file-data'}

The request is sent, however the response is always a 400 error, because it complains the MultipartFile parameter 'binaryFile' is missing: 发送了请求,但是响应始终是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"}'

I've tried adding a 'name' value to both the params and headers of the request but it always comes back with the 400 code. 我尝试将“名称”值添加到请求的参数和标头中,但它总是随400代码一起返回。 Does anyone out there know what I might be doing wrong? 外面有人知道我在做什么错吗?

Actually I eventually figured this out - basically I had a method that formed the header to include the oauth bearer token, along with the ContentType and AcceptType...I then overwrote those with the multipart file info. 实际上,我最终弄清楚了这一点-基本上,我有一个方法形成了包含oauth承载令牌以及ContentType和AcceptType的标头,然后用多部分文件信息覆盖了它们。 THAT was what the receiving REST interface didn't like. 那就是接收REST接口所不喜欢的。 When I just eliminated those header attributes altogether, it seemed to figure it out on its own. 当我完全消除了这些标头属性时,它似乎可以自行解决。

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

相关问题 必需的MultipartFile参数不存在 - Spring Boot REST POST - Required MultipartFile parameter is not present - Spring Boot REST POST 发布错误必需的MultipartFile []参数不存在 - post error Required MultipartFile [ ] parameter not present 必需的MultipartFile参数'file'不存在Java Spring MVC - Required MultipartFile parameter 'file' is not present java spring mvc 在Spring 4.3.1中不存在必需的MultipartFile参数'file' - Required MultipartFile parameter 'file' is not present in spring 4.3.1 错误消息=必需的MultipartFile参数'file'不存在 - Error message = Required MultipartFile parameter 'file' is not present spring mvc 中不存在所需的 MultipartFile 参数“文件” - Required MultipartFile parameter 'file' is not present in spring mvc Postman - 所需的 MultipartFile 参数“文件”不存在 - Postman - Required MultipartFile parameter 'file' is not present 所需的 MultipartFile 参数“文件”不存在 [Postman 和 Springboot] - Required MultipartFile parameter 'file' is not present [Postman and Springboot] 所需的 MultipartFile 参数“文件”不存在 - MultipartyEntityBuilder - Required MultipartFile parameter 'files' is not present - MultipartyEntityBuilder 当前 MultipartFile 的 java.io.FileNotFoundException - java.io.FileNotFoundException for a present MultipartFile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM