I created a simple form in order to upload a single PDF file into a blob in my DB. I'm using thymeleaf + spring boot and dropzone.js.
I'm getting a 405 error - Method not allowed when the file is uploaded.
I' already tried to add a @ResponseBody in my controller method, but the error continues the same.
This is my pdf-upload.html (thymeleaf template):
<html>
<head>
...
<script type="text/javascript">
Dropzone.options.myAwesomeDropzone = {
// url: "[[@{/vouchers/uploadPdfFile}]]",
paramName : "file", // The name that will be used to transfer the file
maxFilesize : 10, // MB
addRemoveLinks : true,
acceptedFiles : ".pdf",
maxFiles : 1,
// headers: {
// 'X-CSRF-TOKEN': $('meta[name="token"]').attr('content')
// }
};
</script>
....
</head>
....
<form th:action="@{/vouchers/uploadPdfFile}"
enctype="multipart/form-data"
method="post" class="dropzone needsclick dz-clickable"
id="my-awesome-dropzone">
<div class="col-12" id="dropzone">
<div class="dz-message needsclick">
Drop the PDF that will be attached to the Voucher...<br>
</div>
</div>
<input type="hidden" id="hd_voucher_id" th:value="${voucher.id}">
</form>
....
</html>
My Controller looks like this:
@RequestMapping(value = "/uploadPdfFile", method = RequestMethod.POST)
public String uploadPdfPost(MultipartHttpServletRequest request) {
try {
Iterator<String> itr = request.getFileNames();
while (itr.hasNext()) {
String uploadedFile = itr.next();
MultipartFile file = request.getFile(uploadedFile);
String mimeType = file.getContentType();
String filename = file.getOriginalFilename();
byte[] bytes = file.getBytes();
FileUpload newFile = new FileUpload(filename, bytes, mimeType);
//fileUploadService.uploadFile(newFile);
}
} catch (Exception e) {
return "vouchers/pdf-upload";
}
return "vouchers/pdf-upload";
}
and when the form is submitted I get the 405 error I mentioned before.
In the log console, I'm getting the following errors:
2019-07-16 15:45:27 WARN o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]
2019-07-16 15:45:27 DEBUG o.s.web.servlet.DispatcherServlet - Exiting from "FORWARD" dispatch, status 405
2019-07-16 15:45:27 DEBUG o.s.s.w.c.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed
2019-07-16 15:45:27 ERROR o.s.b.w.s.support.ErrorPageFilter - Cannot forward to error page for request [/vouchers/uploadPdfFile] as the response has already been committed. As a result, the response may have the wrong status code. If your application is running on WebSphere Application Server you may be able to resolve this problem by setting com.ibm.ws.webcontainer.invokeFlushAfterService to false
Any hints on what's going on here?
Thanks in advance Juan
I found the solution. I'll share it with the community since I spent two days with this issue and maybe this post could help somebody...
First of all, the issue wasn't related to CSRF generation or posting. Was a completely different issue disguised into a 405 error. This is the first conclusion on the matter: you may get 405 error, but it could be generated from a different error (internal server error - 500 in my case).
The reason why I was getting a 500 error is not relevant, what it's important was how I concluded there was a 500 error showed as a 405 one: I simple disabled the csrf token mechanism from spring boot by adding the following lines into my app config (the class which extends WebSecurityConfigurerAdapter):
http
.csrf().disable(); // this will disable the crsf token mechanism from the entire app. Be careful!!
When I disabled it, the 500 error showed up, which was a very simple error (a thymeleaf parse expression error). I solve the 500, then I enable the csrf mechanism again and it worked!
Hope helps somebody in the future.
Regards Juan