Backend, ASP.net Core API:
[Produces("application/json")]
[Route("api/[controller]")]
public class StoriesController : Controller
{
public static List<Story> STORIES = new List<Story>
{
new Story
{
content = "Some really interesting story about dog",
timeOfAdding = new DateTime(2016, 8, 26),
numberOfViews = 11
},
new Story
{
content = "Even cooler story about clown",
timeOfAdding = new DateTime(2016, 9, 26),
numberOfViews = 11
},
new Story
{
content = "And some not cool story",
timeOfAdding = new DateTime(2016, 10, 26),
numberOfViews = 11
}
};
// POST api/values
[HttpPost]
public void Post([FromBody]string value)
{
Story story = new Story
{
content = value,
timeOfAdding = DateTime.Now,
numberOfViews = 0
};
STORIES.Add(story);
}
}
TypeScript function:
add(content: string): Observable<Story> {
let body = JSON.stringify({ "value": content });
//let body = { "value": content };
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers});
return this.http.post(this.heroesUrl, body, options)
.map(this.extractData)
.catch(this.handleError);
}
Parameters sent (seen in Firefox console):
value = null
in Visual Studio 2015 debugger
What's wrong? I've tried everything that I found in internet: adding/removing headers, removing JSON.stringify, adding/removing [FromBody]
attribute. Result is every time the same.