2017-03-04 275 views
0

我在這裏以及其他地方在網上搜索了周圍,似乎無法找到我認爲是我的一個簡單的錯誤的答案。基本上我想通過向遠程機器上的Java REST接口發出Python請求。POST請求來將文件從一臺機器傳輸到另一臺機器。 Java端是這樣的:Python請求POST到java REST接口MultipartFile參數不存在

@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碼。有沒有人知道我可能做錯了什麼?

回答

0

其實我最終想通了 - 基本上我有一個方法,形成頭包括oauth不記名令牌,以及ContentType和AcceptType ...然後我用多部分文件信息覆蓋了這些。這是接收REST接口不喜歡的。當我完全消除這些標題屬性時,它似乎自行解決了這個問題。