簡體   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