简体   繁体   中英

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. The Java side looks like this:

@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:

   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:

'{"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. 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. THAT was what the receiving REST interface didn't like. When I just eliminated those header attributes altogether, it seemed to figure it out on its own.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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