I am new in angular2/ 4 web api. I want to save some data to database using angular 4 web api http. When i call a GET method from angular it works fine, but not working on POST method. I am getting 415 Unsupported Media Type.All the time, but when I use postman the post method is work fine because I change the content type to application/json. when i use alert button i get [object,Object],But in angular2/4 it's not working... I see lots of issues with the same problem, but they don't work for me.
This is my service.ts file
CatchStockDetail(stock: any)
: Observable<IStockdetails[]> {
alert(stock);
let stockdetail = JSON.stringify([{ stock }]);
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this._http.post('http://localhost:1234/api/NewStockCount/' + 'Stock', stockdetail,
{ params: [{ stock }] })
// .map(res => res.json());
// .map(res => res.json().stock)
.map((response: Response) => <IStockdetails[]>response.json())
.catch(this.handleError);
}
This is my component.ts file
this._enqService.CatchStockDetail(this.stock)
.subscribe(value => {
value.forEach(stock => {
this.stockdetail.push(this.stock)
});
},
error => {
console.error(error);
this.statusMessage = "Problem with the service.Please try again after sometime";
});
This is my api code
[HttpPost]
public void Stock([FromBody] List<spGetNewStockCountDetails_Result> jsonvalues)
{
foreach (spGetNewStockCountDetails_Result Datastock in jsonvalues)
{
spGetNewStockCountDetails_Result Stockobject = new spGetNewStockCountDetails_Result();
Stockobject.schid = Datastock.schid;
Stockobject.itemid = Datastock.itemid;
Stockobject.itemcode = Datastock.itemcode;
Stockobject.itemdescription = Datastock.itemdescription;
Stockobject.packingtypeid = Datastock.packingtypeid;
Stockobject.count = Datastock.count;
enqentities.spGetNewStockCountDetails(Datastock.schid, Datastock.itemid, Datastock.itemcode, Datastock.itemdescription, Datastock.packingtypeid, Datastock.count);
}
}
public class spGetNewStockCountDetails
{
public int schid { get; set; }
// public int id { get; set; }
public int itemid { get; set; }
public string itemcode { get; set; }
public string itemdescription { get; set; }
public int packingtypeid { get; set; }
public int count { get; set; }
}
This is my error
"{"Message":"The request entity's media type 'text/plain' is not supported for this resource.","ExceptionMessage":"No MediaTypeFormatter is available to read an object of type