415 (Unsupported Media Type) angular 4 Post
In my case the 415 error was caused because I was calling JSON.stringify(obj)
when there was no need for it. I read somewhere that post method will stringify the body parameter as needed
So instead of this:
this._http
.post(
this.baseUrl + 'home/Save',
JSON.stringify(this.obj),
this.headers
)
I changed it to this:
this._http
.post(
this.baseUrl + 'home/Save',
this.obj, // << no need to stringify
this.headers
)
Here is my actual working code
@Injectable()
export class ParkingService {
constructor(private http: HttpClient) { }
create(parking: Parking) {
const requestUrl = environment.apiUrl + 'parking' ;
const headerOptions = new HttpHeaders();
headerOptions.set('Content-Type', 'application/json');
return this.http.post(requestUrl, parking, {headers: headerOptions}) ;
}
}
This happened to me even after enabling and configuring CORS on the .NET core web api
You need to change the below line
headers = { headers: new Headers({ 'Content-Type': 'application/json' })
};
to
headers={
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
}