How to use CURL to POST Multi-Form File and JSON to Springboot Restful Webservice

Viewed 516

I am trying to use CURL to do a POST request to the following endpoint which takes an html file and a json String as parameter.

Below is the endpoint signature for the first service, which create an html string which is then sent to the second microservice as a 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 {

}

The following curl works with Json embedded in the curl command:

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

But, I will like to read from the json, instead of embedded the data directly e.g.

 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

The second curl command is not working. Its throwing a null pointer in the service.

Please, how can I read from a json file, which is on my mac-os machine and pass as a second parameter to my endpoint, rather than embed the json content which makes the curl command messy. The data is send via a Resttemplate client.

Thanks for your help in advance.

1 Answers

Add ;type=application/json at the end of the json file path as below:

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
Related