3

I have a REST API for uploading files to server written in Java with Spring Boot. I am testing the API through postman. The API is working fine in Linux Environment, but in windows environment it returns 400 Bad Request as response. There is no change in code in both systems, as I am using the same jar file in both environments.

The POST request consist of a file attachment in the form-data and a parameter userName. My API accepts the data in that format only. I have checked the headers in the request in both the machines, and I am just passing a single header Content-Type as application/json.

Could someone guide me, what might be causing the error? I have checked some answers in stackoverflow for what might be the reasons for HTTP 400 other than the endpoint no being existing. Nothing answered my query.

Edit: Adding the code I am using to upload the file.

private static String UPLOADED_FOLDER_BIRTHDAY = "D:/uploads/birthday/";

@PostMapping("/api/upload/birthday")
public ResponseEntity<?> uploadFileForBirthday(@RequestParam("file") MultipartFile uploadfile, @RequestParam("userName") String userName) {

    if (uploadfile.isEmpty()) {
        return new ResponseEntity("please select a file!", HttpStatus.OK);
    }

    try {

        saveUploadedFiles(Arrays.asList(uploadfile), UPLOADED_FOLDER_BIRTHDAY);

    } catch (IOException e) {
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }

    return new ResponseEntity("Successfully uploaded - " +
            uploadfile.getOriginalFilename(), new HttpHeaders(), HttpStatus.OK);

}


    private void saveUploadedFiles(List<MultipartFile> files, String uploadPath) throws IOException {

    for (MultipartFile file : files) {

        if (file.isEmpty()) {
            continue;
        }

        byte[] bytes = file.getBytes();
        Path path = Paths.get(uploadPath + file.getOriginalFilename());
        Files.write(path, bytes);

    }
}
BarathVutukuri
  • 1,265
  • 11
  • 23
  • Please update your question to show what you have already tried in a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve). For further information, please see [how to ask good questions](http://stackoverflow.com/help/how-to-ask), and take the [tour of the site](http://stackoverflow.com/tour) :) – giorgiga Feb 12 '18 at 14:05
  • @giorgiga what could I update in the question for this kind of response. I have looked all the questions tagged with http-status-code-400 in SO. I couldn't get the solution. That too, the issue is just with one OS, not reproducible in other environment. Please tell me what info I could provide to get the solution from the community :) – BarathVutukuri Feb 12 '18 at 14:12
  • There is no clue (not that I can think of) why spring-boot or the JVM would not be portable, so I guess it is safe to assume the issue lies in your code. Post a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) or - at the very least - provide some more info: eg: what is the non-windows machine running? are you running your app behind a webserver? do the two machines have the same default locale? – giorgiga Feb 12 '18 at 14:19
  • BTW: "400 Bad Request" means the request is bad (eg: validation failed) – giorgiga Feb 12 '18 at 14:22
  • What is happening with the file that is uploaded? Are the file contents being written to the file system? You may be running into OS differences if you are doing file system interaction. – M. Rizzo Feb 12 '18 at 15:27
  • a log would help understanding the problem, also the controller code would help together with the code that save the file. – Paizo Feb 12 '18 at 16:26
  • @M.Rizzo the file is not getting written into the file system. I have separate file system paths specified in the code for Windows and Linux OS. – BarathVutukuri Feb 13 '18 at 05:10
  • @Paizo there is no error getting displayed in the log. To be frank, nothing is outputted while throwing that error. Very strange I guess. I have updated the question with the code I am using for upload functionality. – BarathVutukuri Feb 13 '18 at 05:32
  • you catch the `IOException` with no log and return bad request...just put a breakpoint there. Also `UPLOADED_FOLDER_BIRTHDAY ` is not os aware – Paizo Feb 13 '18 at 09:12
  • @Paizo I tried printing the exception using the printstacktrace method and nothing is returned when the response is sent, which is completely strange. Unable to find what might be the issue. I'm still struck with the issue. – BarathVutukuri Feb 16 '18 at 07:52

0 Answers0