i am new to angular 2 and i'm facing a problem which i cant find a solution: When i try to post from angular 2 to API i get - 415 unsupported media type.
Angular 2 code:
onSubmit(value: any) {
// console.log(value.message);
let headers = new Headers({ 'Content-Type': 'application/json'});
let options = new RequestOptions({ headers: headers });
let creds = 'statusuknown';
let body = JSON.stringify(creds);
this.http.post('http://localhost:1318/api/ActionItem', creds)
.subscribe(
() => {console.log('Success')},
err => {console.error(err)}
);
}
And my controller code:
// POST api/actionitem
[HttpPost]
public ActionItem Post( [FromBody]string str)// _id, string _status)
{
ActionItem item = new ActionItem( 313, str);
return item;
}
when i change the controller code to not get data from body it works but refers NULL.
My API call screenshot: Please help & let me know if more details needed.
You defined headers, but didn't use it. Change your code to
this.http.post('http://localhost:1318/api/ActionItem', body, options).map(response => response.json())
.subscribe(
() => {console.log('Success')}
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With