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:
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})
}
CodePudding user response:
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
}