Context
Summarizing, I have an app that sends some file or JSON to a Django server, the server then process the file and create a new zip file. This zip file have to return to the app. I'm trying to do this with a POST request only, returning the file as a response. I was able to download the file as a response with Postman and I can also see the response I want inside one of the HttpErrorResponse attributes. I also found an answer that was able to do the same, but in Java.
Some informations
App: made with Ionic v4 | Server: Django 2.2.4
Front side attempts
Main request method:
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Accept': 'application/json',
}),
requestType: 'application/octet-stream'
};
this.http.post("http://127.0.0.1:8000/converter2/", file, httpOptions)
.pipe(
finalize(() => {
loader.dismiss();
})
)
.subscribe(data => {
console.log(data['body']);
}, error => {
console.log(error);
console.log(error.error.text)
});
Attempts:
With the method above I receive this error:
Error number 1 -> SyntaxError: Unexpected token P in JSON at position 0 at JSON.parse
I'm receiving this error because the http response, by default, is trying to parse the response. I read the documentation of HttpClient, but don't say much about receiving zip files.
I see some answers, like this one and this too, suggesting to add the { responseType: 'blob' }
or { responseType: 'text' }
. Dong this I receive another error:
Error number 2 -> 415 Unsupported Media Type
Trying to send requestType as 'application/zip'
or 'application/octet-stream'
returns the Error number 1. I tried also to change the 'Accept' to these values, resulting in:
Error number 3 -> 406 Could not satisfy the request Accept header
As I said before, I can access the zip file through console.log(error.error.text), but I want to receive it correctly, not as an error.
Server side attempts
Main response method:
# Return processed file
zip_file = open(zipfiles_folder + 'myfile.zip', 'rb')
return_response = HttpResponse(zip_file, content_type='application/force-download')
return_response['Content-Disposition'] = 'attachment; filename="%s"' % 'myfile.zip'
return return_response
Attempts:
Now I tried to change the response content_type
, without success, as the following:
- content_type='application/zip'
- content_type='application/octet-stream'
Then I tried to follow some suggestions, again without success:
zip_file = open(zipfiles_folder + 'myfile.zip', 'rb')
return_response = HttpResponse(zip_file, content_type='application/force-download')
return_response['Content-Disposition'] = 'attachment; filename="%s"' % 'myfile.zip'
return_response['Content-Description'] = 'File Transfer'
return_response['Content-Transfer-Enconding'] = 'binary'
return return_response
The ZIP response (not exactly this, but pretty similar):
PK B�O<�'� � filename.shp '
N� `�_I�*-!L��;� `�YI�ޞ�b��;�
`�YI�ޞ�b��;�
`�_I�*-!L��;�PK B�O�f#t t filename.shx '
:� `�_I�*-!L��;� `�YI�ޞ�b��;� 2
@
PK B�Opk��Z Z filename.dbfw A W FID N
0 1PK B�O�Z�� � filename.prjPROJCS["SIRGAS_2000_UTM_Zone_22S",GEOGCS["GCS_SIRGAS_2000",DATUM["D_SIRGAS_2000",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",10000000.0],PARAMETER["Central_Meridian",-51.0],PARAMETER["Scale_Factor",0.9996],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]PK B�O<�'� � �� filename.shpPK B�O�f#t t ��� filename.shxPK B�Opk��Z Z ��d filename.dbfPK B�O�Z�� � ��� filename.prjPK � �