title: How to send @Requestbody and @Requestpart together in spring
body: <p>I want to pass json and file together in the controller using curl. I have following method in controller.</p>
  
  <pre><code>@PostMapping(value = /api/campaign, headers = {"content-type=multipart/mixed","content-type=multipart/form-data"})
      @ResponseBody
      @ResponseStatus(HttpStatus.CREATED)
      public @Valid ResponseDTO campaignCreator (@Valid @RequestBody CampaignCreatorDTO campaignCreatorDTO, @RequestPart("file") MultipartFile adGraphic){
  }
  </code></pre>
  
  <p>Below is the curl command</p>
  
  <pre><code>curl -i -X POST -H "Content-Type: multipart/mixed" -d "campaignCreatorDTO={\"edipi\":123456789,\"firstName\":\"John\",\"lastName\":\"Smith\",\"email\":\"john.smith@gmail.com\"};type=application/json" -F "file=@newfile.png;type=image/png"  http://localhost:8080/api/campaign
  </code></pre>
  
  <p>controller method is not getting called.
  when I put json in a file and use file in curl command in place of direct json it works.
  But I do not want to use file for json.</p>
  
  <p>I tried to use @RequestPart for json object but same issue.</p>
  
  <p>is there any way to pass multipart file inside json I mean CampaignCreatorDTO object?</p>
  
  <p>Update:: Now I am able to pass RequestPart for both type but image size is coming 0 bytes. Though iamge is present in the filesystem.</p>
  
  <p>Updated Code ::
  now using below code, and getting filesize as 0 bytes.</p>
  
  <pre><code>@PostMapping(value = /api/campaign, consumes = {"multipart/form-data","multipart/mixed"})
      @ResponseBody
      @ResponseStatus(HttpStatus.CREATED)
      public @Valid ResponseDTO campaignCreator (@Valid @RequestPart("json") CampaignCreatorDTO campaignCreatorDTO, @RequestPart("file") MultipartFile adGraphic) {
  }
  </code></pre>
  
  <p>I tried solution given in this link but still same issue</p>
  
  <p><a href="https://stackoverflow.com/questions/21329426/spring-mvc-multipart-request-with-json">Spring MVC Multipart Request with JSON</a></p>
  
  <p>this is how client is paasing the data to server</p>
  
  <pre><code>let formData = new FormData()
  
  const blob = new Blob([json], {
  type: 'application/json'
  });
  
  formData.append("json", blob)
  formData.append("file", values.adCreativeImageCover)
  
  let authToken = sessionStorage.getItem("authToken")
  
  fetch(/api/campaign, {
  method: "POST",
  headers: {
  'Accept': 'application/json',
  'X-Auth-Token': authToken,
  },
  mode: 'cors',
  body: formData
  })
  </code></pre>