1

this is the spring boot API endpoint which receives request from react js frontend

@PostMapping(value = "/upload", consumes = {MediaType.APPLICATION_JSON_VALUE, 
MediaType.MULTIPART_FORM_DATA_VALUE})
public ResponseEntity<MessageResponse> uploadFile(@RequestPart("fileDB") String fileDB, 
@RequestPart("file") MultipartFile file) {
  String message = "";
  try {
    storageService.store(file, fileDB);

    message = "Uploaded the file successfully: " + file.getOriginalFilename();
    return ResponseEntity.status(HttpStatus.OK).body(new MessageResponse(message));
  } catch (Exception e) {
    message = "Could not upload the file: " + file.getOriginalFilename() + "!";
    return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new 
 MessageResponse(message));
  }
}
Victor
  • 11
  • 4
  • Does this answer your question? [Can we use multipart and @RequestBody together in spring?](https://stackoverflow.com/questions/40924649/can-we-use-multipart-and-requestbody-together-in-spring) – Justin Mathew Feb 11 '22 at 17:29
  • the endpoint above works, I actually needed the frontend react side, how to push the elements from react. – Victor Feb 11 '22 at 19:28
  • 1
    when I use postman, am actually able to post both image and json data – Victor Feb 11 '22 at 19:31
  • Very similar to this question that I answered recently: https://stackoverflow.com/a/71006941/107976 – poeticGeek Feb 13 '22 at 11:48
  • const upload = (file, fileDB, onUploadProgress) => { const formData = new FormData(); formData.append('file', file); formData.append('fileDB', fileDB); return axios.post(`${API_URL}upload`, formData, { headers: { 'Content-Type': 'multipart/form-data' }, onUploadProgress }); }; – Victor Feb 14 '22 at 08:06
  • this is how am trying to post but only file contents is sent, the other fields in 'res' are hitting the table with blank values – Victor Feb 14 '22 at 08:08

0 Answers0