I am trying to expose an api which downloads files can be of different types, I am showing excel for an example. The file is successfully getting downloaded with GET method endpoint but when i do the same with POST method it doesn’t download it but i can see contents of the file in network tab as texts. I tried removing all logic and doing it with empty file and it is same. sample code shared. This backend api is getting called from a frontend application page written in REACT.
@GetMapping("/downloadGet")
public void downloadManualBulkRefund1( DownloadRequest
downloadRequest,HttpServletResponse response) throws Exception {
FormatterOutputTO output = new FormatterOutputTO();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;
filename="sample_18082023.xlsx"");
response.getOutputStream().write(output.getByteStream() != null ?
output.getByteStream() : new byte[] {});
response.getOutputStream().flush();
response.getOutputStream().close();
}
For Post method everything is same except it is a post method.
2
Answers
What is the log in your backend server saying ?
You have to change your method return type from Void to resource type by mentioning a Generic partof the response entity class. Ref example
External Ref for more detials : https://www.codejava.net/frameworks/spring-boot/file-download-upload-rest-api-examples