I am trying to post some JSON in the web api.net core, but for some reason it is always null, no matter what I tried, see below.
this is my .net server-side web api code
[Produces("application/json")]
[Route("api/v1/Issues")]
[Authorize]
[EnableCors("AllowCors")]
public class IssuesController : Controller
{
[HttpPost]
public IActionResult PostIssues([FromBody] string issue)
{
}
}
and this is how I try to send a message via angular (note that the corresponding services are loaded)
public addIssue(issue) {
const headers = new Headers();
this.authService.getToken().toPromise().then( t => {
headers.append('Authorization', `Bearer ${t.toString()}`);
headers.append('Content-Type', 'application/json')
this.http.post(`${this.baseUrl}/v1/issues`, issue, {
headers: headers
}).toPromise()
.then(res => {
console.log(res.json());
});
});
}
I tried changing the message to
this.http.post(`${this.baseUrl}/v1/issues`, JSON.stringify(issue))
and even
this.http.post(`${this.baseUrl}/v1/issues`, {'issue', JSON.stringify(issue)})
but nothing works, does anyone know why this is happening? For the explanation string issueobtained in the API, there is always a null value. Here is a successful answer, when I get to the server, you can see that the request payload is not null, but reaches the web api as null

1
, , 4.5 .net, , .net, , foo
public class Foo
{
public string Name { get; set; }
public string Json { get; set; }
}
,
[HttpPost]
public IActionResult PostIssues([FromBody] Foo issue)
{
Debug.Write(issue);
return Ok(issue);
}
angular
public addIssue(issue) {
const headers = new Headers();
this.authService.getToken().toPromise().then( t => {
headers.append('Authorization', `Bearer ${t.toString()}`);
headers.append('Content-Type', 'application/json');
this.http.post(`${this.baseUrl}/v1/issues`, {'name' : 'name', 'json': JSON.stringify(issue)}, {
headers: headers,
withCredentials: true
}).toPromise()
.then(res => {
console.log(res.json());
});
});
}

, Json ? 4.5, .net?