I want to upload so files from my Angular 5 frontend. I am using the Django Rest Framework in my backend.
This is my Angular service to upload the file:
uploadStudentFile(solution_id: number, file: File): Observable<SolutionRealFile> {
let url = `${this.url}/solutions/${solution_id}/file_uploads/`;
const formData: FormData = new FormData();
formData.append('file_src', file, file.name);
return this.httpClient.post<SolutionRealFile>(url, formData, { headers: this.headers })
.pipe(
catchError(this.handleError('Upload file', new SolutionRealFile()))
);
}
When running the request from my app in the browser, the request fails with HTTP/1.1" 400 73
and this is the error message:
{"detail":"JSON parse error - Expecting value: line 1 column 1 (char 0)"}
But running the same request with the same file form Postman, everything is working. I am new to Angular 5, maybe is just a small problem not sure.
Edit 1:
The headers:
'Content-Type': 'application/json',
'Authorization': `JWT ${localStorage.getItem(TOKEN_NAME)}`