CURL模擬Http請求上傳文件和JSON數據

       最近在項目開發時,爲了支持系統在持續集成時進行自動化,需要使用CURL模內系統的Spring Restful接口。系統中有個接口的功能是要滿足同時上傳文件和該文件的描述信息。經多次查閱資料後,找到如下解決辦法,現分享給大家。

1. SpringRestful接口

@RequestMapping(method = RequestMethod.PUT,
            consumes = {"multipart/form-data"})
    @ResponseBody
    public Result updateAlgorithm(
            @RequestPart(value = "pluginFile", required = false)
                    MultipartFile pluginFile,
            @RequestPart("algorithm") Algorithm algorithm) {
        return Result.success(getAlgorithmManager().updateAlgorithm(
                algorithm, pluginFile, getCurrentUser()));
    }

2. POST請求

3. CURL命令

  curl -v -X PUT \
    http://shareone-sandbox1.baidu-int.com/ds/v1/algorithms \
    -H 'Authorization: eyJhbGciOiJSUzI1NiJ9.eyJ1c2VybmFtZSI6InN1bnNvbmcwMSIsImV4cCI6MTY3MTcwNDczM30.SiDdIqGX0APj2CwZtrKPhB8QP6nHbF1DFxO8UDu9z5uoh-jRoyZMgWrdYIJRDmTc_DLal-9rNEJ6XCnPili0qQIOgP_MDpr-HHVK0n2eQMlxZg_5smCf5ao-pG2VCVIy-xE5fUz-JbmRZM7TDxlubsg5erZIc9zRaSIxxJwXFw7k_EGakchiwAaMe7pbNnUfk7CSyBn1em2PD0AJMf0Y6GmHshTD9T_cHH8s338g0eCs3U0a6FxswozXwsHlXHElAYSc-0n6RiDLd2ogyJSR_6cJCiHs1NX0RTgWB9PV8AI5SGiWf_MU5YnhTWVamN4r_9jP_bWodwg7jAk95aWPvg' \
    -H 'cache-control: no-cache' \
    -H 'Content-Type: multipart/form-data' \
    -F 'pluginFile=@/Users/psb/download/smc-psi-plugin-release-2.0.2.0.tar.gz;type: application/x-gzip' \
    -F 'algorithm={"algorithmName":"psi","algorithmType":"dataVerify","algorithmTags":["SMC"],"partnerCategory":"2","creator":"psb","sgxEnabled":0,"description":"","algorithmId":"algorithm-1575872280736-41"};type=application/json'

或者

 curl -v -X PUT \
   http://shareone-sandbox1.baidu-int.com/ds/v1/algorithms \
   -H 'Authorization: eyJhbGciOiJSUzI1NiJ9.eyJ1c2VybmFtZSI6InN1bnNvbmcwMSIsImV4cCI6MTY3MTcwNDczM30.SiDdIqGX0APj2CwZtrKPhB8QP6nHbF1DFxO8UDu9z5uoh-jRoyZMgWrdYIJRDmTc_DLal-9rNEJ6XCnPili0qQIOgP_MDpr-HHVK0n2eQMlxZg_5smCf5ao-pG2VCVIy-xE5fUz-JbmRZM7TDxlubsg5erZIc9zRaSIxxJwXFw7k_EGakchiwAaMe7pbNnUfk7CSyBn1em2PD0AJMf0Y6GmHshTD9T_cHH8s338g0eCs3U0a6FxswozXwsHlXHElAYSc-0n6RiDLd2ogyJSR_6cJCiHs1NX0RTgWB9PV8AI5SGiWf_MU5YnhTWVamN4r_9jP_bWodwg7jAk95aWPvg' \
   -H 'cache-control: no-cache' \
   -H 'Content-Type: multipart/form-data' \
   -F 'pluginFile=@/Users/psb/download/smc-psi-plugin-release-2.0.2.0.tar.gz;type: application/x-gzip' \
   -F 'algorithm=@/Users/psb/download/algorithm.json;type=application/json'

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章