I am calling a rest api from jquery ajax client and expecting both json as well as binary data (tar.gz file) to be returned in the same response. However the binary data that I receive has some extra bytes and the tar.gz file that I save is not in correct format and cannot be uncompressed. I am setting the response type of the ajax request to responseType="multipart/form-data"
This seems to work fine if I individually call the binary file with the responseType="blob" . In this case I am getting the correct binary data and able to save the tar.gz file in the correct format, so I know there is no issue in the backend code.
However I need both the json and binary data in the same response. The code that handles the response is as follows:
var multiPartData = this._multiPartParse(response, contentType);
var jsonResponse;
var binaryResponse;
if (multiPartData) {
if(multiPartData["application/json"]) {
jsonResponse = JSON.parse(multiPartData["application/json"]);
}
if(multiPartData["application/octet-stream"]) {
binaryResponse = multiPartData["application/octet-stream"];
var content = new Blob([binaryResponse], {type : "application/octet-stream"});
saveAs(content, "download.tar.gz");
}
}
In this request, if I set the responseType='blob', the json data is not returned, and if I set the responseType='multipart/form-data' then the binary data is not correct. I have tried responseType='multipart/mixed' and that does not help either.
I relatively new to javascript. What am I missing here?