I am currently working on a web application using AngularJS and SpringBoot API REST.
My challenge involves calling a REST API with a GET request to retrieve a flow of text data, and then attempting to save this data in a file with the charset WINDOWS-1250.
Scenario :
1) The AngularJS client initiates a GET request to the API REST
2) The API REST reads from a database, processes the data, and returns the result in the response body
3) The AngularJS client tries to save the result in a file with charset WINDOWS-1250.
However, when I attempt to save the file using FileSaver.saveAs, the file ends up being encoded in UTF-8 instead of Windows-1250, despite specifying the charset as Windows-1250.
Can anyone offer assistance with this issue?
Angular Test Script:
$http({method:'GET',
url: $scope.url + "/cmpcode/" + this.demand.company + "/pcmcode/" + this.demand.pcmcode + "/prlcode/" + this.demand.prlcode + "/user/" + this.demand.user,
responseType : "blob" ,
headers:{'X-Auth-Token': sessionService.getLogonDatas.token}})
.then(function(response) {
$scope.showErrorAlert = false;
var data = new Blob([response.data], { type: 'text/plain; charset=Windows-1250' });
FileSaver.saveAs(data, "MyFile.test");
$scope.showSuccessAlert = true;},
function(rejection) {
$scope.showSuccessAlert = false;
if (rejection.status === 406){$scope.message.code = "NO_ROWS";}
$scope.showErrorAlert = true;});
};
SpringBoot Test APIREST:
public void getPaymentNationalFile(HttpServletResponse response,
@PathVariable("cmpcode") @Length(max = 12) @NotNull String companyCode,
@PathVariable("pcmcode") @Length(max = 12) @NotNull String pcmcode,
@PathVariable("prlcode") @Length(max = 12) @NotNull String prlcode,
@PathVariable("user") @Length(max = 12) @NotNull String user,
{
//Without Try & Catch
PaymentNationalParam paymentNationalParam = new PaymentNationalParam(companyCode, pcmcode, prlcode, user);
paymentNationalData = paymentNationalService.GetFilePaymentNational(paymentNationalParam);
response.setContentType("text/plain;charset=" + paymentNationalData.getCharacterEncoding()); //Here "Windows-1250"
int longueur = 0 ;
byte[] buffer = null;
ServletOutputStream servletOutputStream = response.getOutputStream();
for (String string : paymentNationalData.prepareLignePaymentToWrite())
{
buffer = (string + "\n").getBytes() ;
longueur += buffer.length;
servletOutputStream.write(buffer , 0, buffer.length);
}
response.setContentLength(longueur);
servletOutputStream.flush();
}
API Response (extract)
Header :
Content-Type : text/plain;charset=windows-1250
Transfer-Encoding : chunked
X-Content-Type-Options : nosniffBody extract -> line 1 and 2 of the response (more lines): 110,20170626,2594917,10500086,0,"29105000861000002274422993","16105010701000000101025518","NORAUTO POLSKA SP Z.O.O|UL. JUBILERSKA 10||04190 WARSZAWA","AUTOLAND
43-25|MICKIEWICZA 28| 0|43-250 PAWĹOWICE",0,10501070,"114024","","","51",""110,20170626,70725,10500086,0,"29105000861000002274422993","58103015080000000502563009","NORAUTO POLSKA SP Z.O.O|UL. JUBILERSKA 10||04190 WARSZAWA","BOSCH ROBERT SP02-23|JUTRZENKI 105| 0|02-231 WARSZAWA",0,10301508,"114025","","","51",""