欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CURL模拟Http请求上传文件和JSON数据

程序员文章站 2024-01-20 21:10:04
...

       最近在项目开发时,为了支持系统在持续集成时进行自动化,需要使用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请求

CURL模拟Http请求上传文件和JSON数据

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 'aaa@qq.com/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 'aaa@qq.com/Users/psb/download/smc-psi-plugin-release-2.0.2.0.tar.gz;type: application/x-gzip' \
   -F 'aaa@qq.com/Users/psb/download/algorithm.json;type=application/json'

 

相关标签: Linux Spring Boot