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.
Content-Type
header when you use Postman. You only send a string in your example, so you actually don't need JSON. You can sendcreds
directly. – a better oliver