Attempting to send data to my asp.net server using angular. After testing the front-end data, the issue arises when sending the data with a post request; angular sends null data instead. Interestingly, when sending the same data to the server from Postman, it works perfectly fine and the response is true, as expected. Unsure of what might be missing in the front-end setup...
Snippet of angular code with post request:
// Set admin ID to object and create new school
schoolData.AdminId = 0;
const schoolHeaders = { 'Content-Type': 'application/json' };
const schoolBody = schoolData;
this.http.post<any>(this.baseUrl + "Api/CreateSchool", { body: JSON.stringify(schoolBody) }, { headers: schoolHeaders }).subscribe(schoolResponse => {
if (schoolResponse) {
this.router.navigate(['/home']);
}
}, error => console.error(error));
Corresponding asp.net code snippet:
[HttpPost]
[Route("Api/CreateSchool")]
public bool CreateNewSchool([FromBody] SchoolModel schoolData)
{
// IT PRINTS THE NAME ONLY FROM POSTMAN NOT IF THE REQUEST IS FROM ANGULAR
Console.WriteLine("Name is: " + schoolData.Name);
SchoolManager context = HttpContext.RequestServices.GetService(typeof(SchoolManager)) as SchoolManager;
return context.CreateNewSchool(schoolData);
}