.net-core Angular: HTTP Post Error 400 with Angular HTTPClient

Issue

I want to pass a newly created object to my C# controller. But I always get a server error 400 on the HTTP Post Request I make via Angular.

I make a HTTP GET request in the same way but it works without problems.
My C# Controller:

    [Authorize]
    [ApiController]
    [Produces("application/json")]
    [Route("[controller]")]
    public class SiteSettingsController : ControllerBase
    {
        [HttpGet]
        public IEnumerable<SiteSettings> Get()
        {
            return GetAllSiteSettings();
        }

        [HttpPost]
        public Task<IActionResult> Post([FromBody]SiteSettings siteSettings)
        {
            try
            {
                return null;
            }
            catch (Exception e)
            {
                return null;
            }
            return null;
        }

     }

And my Angular Reuqest:

save() {
    const settings: SiteSettings = {
      id: '',
      name: 'Test',
      domain: 'test.local',
      active: false
    };

    const body = JSON.stringify(settings);
    const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json'
      })
    };

    return this.http.post<any>(this.baseUrl + 'sitesettings', body, httpOptions).subscribe({
      error: error => console.error('There was an error!', error)
    });
  }

Solution

I was completely stupid and missed the actual error message.
So stay focused and look closely…

0: "The JSON value could not be converted to System.Guid. Path: $.id | LineNumber: 0 | BytePositionInLine: 8."

Answered By – n00dle

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published