Home > Blockchain >  Error 415 on angular although the headers are up
Error 415 on angular although the headers are up

Time:12-22

I'm getting error 415 on angular api request but http header are set:

login.service.ts:

var httpHeaders = new HttpHeaders();
httpHeaders.set('Content-Type', 'application/json');
httpHeaders.set('Access-Control-Allow-Origin', '*');
return this.httpClient.post<any>(requestUrl, params, { headers: httpHeaders })

As backend i used django rest framework Any idea ? Thans in advance.

CodePudding user response:

HttpHeaders class is immutable in Angular so now you did not really set any headers. You need to assign the result of set operation to you variable.

let httpHeaders = new HttpHeaders();
httpHeaders = httpHeaders.set('Content-Type', 'application/json');
// depending on the server settings might also be needed 
httpHeaders = httpHeaders.set('Accept', 'application/json');
httpHeaders = httpHeaders.set('Access-Control-Allow-Origin', '*');
return this.httpClient.post<any>(requestUrl, params, { headers: httpHeaders })
  • Related