3

Here is a very dramatic situation for me, I don't what mistake supplying payload to rest client, because of which I am getting "400: BAD REQUEST" exception. Below is the code please help me solve it,

@RequestMapping(value = "/uploadQuestionImg", method = RequestMethod.POST)
    public ResponseEntity<FileMetadata> commonFileUpload(@RequestParam("file") MultipartFile file) {
        FileMetadata fileInfo = null;
        HttpStatus statusCode = HttpStatus.BAD_REQUEST;
        if (!file.isEmpty()) {
            try {
                FileInfoService reqFile = createFileInfo(file, "");
                fileInfo = (FileMetadata) operations.store(file.getInputStream(), reqFile);
                statusCode = HttpStatus.OK;
            } catch (Exception e) {
                logger.warn("Exception occured", e);
                statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
            }
        } else {
            logger.warn("Invalid request");
        }
        return new ResponseEntity<FileMetadata>(fileInfo, statusCode);
    } 

Here is the payload I am passing to the rest client,

enter image description here

Shamim Ahmad
  • 808
  • 3
  • 22
  • 40

1 Answers1

1

400: BAD REQUEST error will arise when you try to request with the parameters that will not supported by the API.In your case your trying to POST a request ie , Multi part file upload request.the request should be in format of multi part file request else the server will throw 400: BAD REQUEST

U can try http://www.codejava.net/java-se/networking/upload-files-by-sending-multipart-request-programmatically you can test multi part file request with this code.

Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
  • I have already added a filter for multipart request: ** multipartFilter multipartFilter org.springframework.web.multipart.support.MultipartFilter multipartFilter /* ** – Shamim Ahmad Jul 31 '15 at 12:25
  • **org.springframework.web.multipart.support.MultipartFilter** class to send muiltipart request... – Shamim Ahmad Jul 31 '15 at 12:27
  • If I am not wrong I have only one parameter to pass which i am passing "@RequestParam("file") MultipartFile file". What else you think may be wrong. – Shamim Ahmad Jul 31 '15 at 12:47