Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Angular – Value posted to .NET Framework API is always null

I am trying to post something on my API.
In the moment a test JSON would be enough for a little feeling of success.

Originally I want to post an object like so:

sendRezept(rezept:Rezept) {
  postHeader =  new HttpHeaders()
    .set('Content-Type', 'application/json')
  return this.http.post<string>(this.rUrl, rezept, {headers:postHeader})
}

The POST function of my API looks like that:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public string Post([FromBody] string value)
{
    if (value != null)
    {
        value = value.ToString();
        return "yay";
    }
    return "damned";
}

Each and everytime whatever I tried the value is null and "damned" is returned.

I read, that there is no ‘_’ allowed in JSON so I made an interface of rezept, which I recently tried pushing. It looks like this:

sendRezept(rezept:Rezept) {
  let medikamente = rezept.combineMedikamente();
  let meds:any[] = [];
  for (let i = 0; i<medikamente.length; i++){
    if (medikamente[i].name == '')
      meds[i] = null;
    else 
    meds[i] = medikamente[i]
  }
  let irezept:IRezept = {
    kunde:rezept.kunde,
    krankenkasse:rezept.krankenkasse,
    arzt:rezept.arzt,
    isGebFrei:rezept.isGebFrei,
    isGebPfl:rezept.isGebPfl,
    isNoctu:rezept.isNoctu,
    isSonstige:rezept.isSonstige,
    isUnfall:rezept.isUnfall,
    isArbeitsunfall:rezept.isArbeitsunfall,
    ausstelldatum:rezept.ausstelldatum,
    unfalldatum:rezept.unfalldatum,
    arbeitgeber:rezept.arbeitgeber,
    medikament1:meds[0],
    medikament2:meds[1],
    medikament3:meds[2],
    title:rezept.title
  }
  return this.http.post<string>(this.rUrl, irezept, {headers:this.postHeader})
}

>Solution :

Your controller API action should expect to receive an object, not a string.

[HttpPost]
public string Post([FromBody] Rezept irezept)
{
    ...
    // Access property: irezept.Kunde
}
public class Rezept
{
    public string Kunde { get; set; }
    // Properties
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading