I am working on Angular 2 technology, in one of my current project I was faced the issue in HTTTP POST request using typescript code.
This is the code I wrote in myservice.ts
postVehicleInfoRestful(vehicleInfo: VehicleInfoTable): Observable<VehicleInfoTable> {
console.log('VehicleInfo Service.')
//let body = JSON.stringify(vehicleInfo ? vehicleInfo : null);
let body = JSON.stringify(vehicleInfo);
console.log(body);
let headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded'
}); //'application/x-www-form-urlencoded' application/json
let options = new RequestOptions({ headers: headers });//, method: "post"
console.log(this._settingsService.todoListAPIUrl + '/api/VehicleInfoTable');
return this._http.post(this._settingsService.todoListAPIUrl + '/api/VehicleInfoTable', body, options)
.map(res => res.json())
.do(data => console.log(JSON.stringify(data)))
.catch(this.handleError);
}
And also, wrote the code in mycomponenet.ts
ngOnInit(): void {
console.log(' VehicleInfoComponent In OnInit');
this.vehicleInfo.ID = 7;
this.vehicleInfo.AssetType = 'Auto';
this.vehicleInfo.IntendedUse = 'Personal';
this.vehicleInfo.NeworUsed = 'New';
this.vehicleInfo.SSN = '123456789';
this.vehicleInfo.VehicleYear = '1';
this.vehicleInfo.VehicleMake = '2';
this.vehicleInfo.VehicleModel = '3';
this.vehicleInfo.VIN = 'US123';
this.vehicleInfo.CurrentMileage = '40';
}
saveVehicleInfo() {
console.log('Save Vehicle Info Details.');
console.log(this.vehicleInfo);
this._vehicleInfoService.postVehicleInfoRestful(this.vehicleInfo).subscribe(//call the post
data => this.postVehicleInfoToServer = JSON.stringify(data),// put the data returned from the server in our variable
error => console.log("Error HTTP Post Service"),// in case of failure show this message
() => console.log("Job Done Post !")); //run this code in all cases
}
This is the code I wrote in mycontroller.cs
public async Task Post(VehicleInfoTable vehicleInfo)
{
try
{
//owner = ((ClaimsIdentity)User.Identity).FindFirst(ClaimTypes.NameIdentifier).Value;
using (var client = NewDataAPIClient())
{
await client.VehicleInfoTables.PostVehicleInfoTableByVehicleinfotableAsync(new VehicleInfoTable
{
ID = (int)vehicleInfo.ID,
AssetType = vehicleInfo.AssetType,
IntendedUse = vehicleInfo.IntendedUse,
NeworUsed = vehicleInfo.NeworUsed,
SSN = vehicleInfo.SSN,
VehicleYear = vehicleInfo.VehicleYear,
VehicleMake = vehicleInfo.VehicleMake,
VehicleModel = vehicleInfo.VehicleModel,
VIN = vehicleInfo.VIN,
CurrentMileage = vehicleInfo.CurrentMileage
});
}
System.Diagnostics.Trace.TraceInformation("VehicleInfoTableController in ToDoListAPI");
DevInsights.TrackEvent("VehicleInfoTableController in ToDoListAPI", "Post");
}
catch (Exception ex)
{
DevInsights.TrackException(ex, "VehicleInfoTableController in ToDoListAPI", "Post");
System.Diagnostics.Trace.TraceWarning(ex.Message);
}
}
when I debug my code, once click on save button, based on my post url this method will fired public async Task Post(VehicleInfoTable vehicleInfo) { ...... } but in that vehicleInfo parameter contains all fields value will be null but when I make post request I added the data in the body.
Can you please tell me where I did mistake for adding the data to body in http post request.
Can you please tell me where I did mistake and how can I resolve this issue?
-Pradeep
