Search code examples
javaspringrestmultipartform-datafiddler

Multipart with JSON in spring rest is not working


I am trying to create a method which accepts both multipart/form-data and application/json content type in same request. I am using Spring Rest for creating the same. But the call does not reach the method when I requested from Fiddler. Please help

Service method

@RequestMapping(value = "/", method = RequestMethod.POST, produces = "application/json",consumes = "multipart/form-data")
public ResponseEntity<JSONObject> addField( @RequestParam int customerId,@RequestParam int teId, 
        @RequestPart("file") List<MultipartFile> multipartFiles, @RequestParam("toast") String toast, MultipartHttpServletRequest request){

Fiddler

Header

Content-Type: multipart/form-data; boundary=HereGoes;

Body

 Content-Type: application/json

 ---------------------------acebdf13572468
  Content-Disposition: form-data; name="fieldNameHere"; filename="file1.pdf"
Content-Type: application/pdf

  <@INCLUDE *C:\Users\User\Desktop\file1.pdf*@>
 ---------------------------acebdf13572468--

Solution

  • Add correct file name in request body of fiddler. In your method it is @RequestPart("file"). So use "file" in "name" field instead of "fieldNameHere" which is generated by fiddler. Your request body should look like below.

     ---------------------------acebdf13572468
     Content-Disposition: form-data; name="file"; filename="file1.pdf"
     Content-Type: application/pdf
    
     <@INCLUDE *C:\Users\User\Desktop\file1.pdf*@>
     ---------------------------acebdf13572468--