Attempted to post data to an API, but received a 400 bad request error. After testing with Postman, it seems that the issue may lie within my service or TypeScript code. As a newcomer to Angular, I am seeking assistance as I have searched extensively without finding a solution. The API expects values in JSON format (Note: using Angular4 Web API).
{
"Header": {
"UserID": 6,
"created": "February 4, 2016 10:13:00",
....etc........
},
"detail": [
{
"ShopID": 1,
"ItemID": 126,
.....etc.........
},
{
"ShopID": 1,
"ItemID": 127,
.....etc.........
}
]
}
Data being passed to the API looks like this (potentially causing the problem):
[{
"userid": "64",
"created": "February 4, 2016 10:13:00",
"CompanyID": "1",
"modified": "February 4, 2016 10:13:00",
"modifieduserid": "65",
"confirm": "true",
"ShopId": 1,
"TransactionId": 1,
"shopid": 12,
"Itemid": 12928,
"PackingTypeID": 3,
"Itemcode": "124018",
"Itemdescription": "COTTON GLOVES 350G",
"PackingtypeName": "12 PCS",
"Stockcount": 12
}
This is my TypeScript file:
stockitems: IStockCountHeaders[] = [];
items: IStockCountHeaders;
stockdetail: IStockdetails[] = [];
addItems(value: any) {
this.items = new IStockCountHeaders(this.userid, this.created, t this.confirm,this.shopid,
value.ItemID, value.PackingTypeID, value.ItemCode, value.ItemDescription,
value.PackingtypeName,
value.Stock,
);
this.stockitems.push(this.items);
}
onclick(){
this._enqService.CatchStockDetailHeader(this.stockitems)
.subscribe(value => {
if (typeof value !== 'undefined' && value != null) {
value.forEach(items => {
this.stockitems.push(this.items)
});
}
},
error => {
console.error(error);
this.statusMessage = "Problem with the service.Please try again after sometime";
});
}
This is my Angular service file:
CatchStockDetailHeader(stock: any)
: Observable<IStockCountHeaders[]> {
let IStockCounts = stock;
console.log(IStockCounts)
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
headers.append('userid', IStockCounts.userid);
headers.append('created', IStockCounts.created);
headers.append('.CompanyID', IStockCounts.CompanyID);
headers.append('modified', IStockCounts.modified);
headers.append('modifieduserid', IStockCounts.modifieduserid);
headers.append('confirm', IStockCounts.confirm);
return this._http.post('http://localhost:3071/api/Stockcountheader/' + 'Stock', IStockCounts, options)
.map((response: Response) => <IStockCountHeaders[]>response.json())
.catch(this.handleError);
}