首页 > 解决方案 > 如何使用 CURL 将多格式文件和 JSON 发布到 Springboot Restful Webservice

问题描述

我正在尝试使用 CURL 向以下端点发出 POST 请求,该端点以 html 文件和 json 字符串作为参数。

下面是第一个服务的端点签名,它创建一个 html 字符串,然后作为 json 发送到第二个微服务:

@PostMapping(path = "/pdf-template", produces = APPLICATION_JSON_VALUE)
    public ResponseEntity<String> bindTemplateData(
            @ApiParam(value = "BindDataRequest payload", required = true)
            @RequestParam MultipartFile template, @RequestParam String templateDataJson) throws IOException {

}

以下 curl适用于 curl 命令中嵌入的 Json:

curl --form 'template=@/Users/joe.deen/ms-template-binder/src/main/resources/html-template/Template.html' --form 'templateDataJson={"cost":"2.00","name":"James Milner 2"}' http://localhost:8081//pdf/download | base64 -D > test.pdf

但是,我想从 json 中读取,而不是直接嵌入数据,例如

 curl --form 'template=@/Users/joe.deen/ms-template-binder/src/main/resources/html-template/Template.html' --form 'templateDataJson=@/Users/joe.deen/ms-template-binder/src/main/resources/json/test.json' http://localhost:8081//pdf/download | base64 -D > test.pdf

第二个 curl 命令不起作用。它在服务中抛出一个空指针。

请,我如何从我的 mac-os 机器上的 json 文件中读取并作为第二个参数传递给我的端点,而不是嵌入使 curl 命令混乱的 json 内容。数据通过 Resttemplate 客户端发送。

提前感谢您的帮助。

标签: javaspring-bootcurlpdf-generationdevops

解决方案


在 json 文件路径末尾添加;type=application/json如下:

curl --form 'template=@/Users/joe.deen/ms-template-binder/src/main/resources/html-template/Template.html' --form 'templateDataJson=@/Users/joe.deen/ms-template-binder/src/main/resources/json/test.json;type=application/json' http://localhost:8081//pdf/download | base64 -D > test.pdf

推荐阅读