Angular 2 HTTP post to Web API without [FromBody]


I have a sample Angular 2 code below.

 this.headers = new Headers({ 'Content-Type': 'application/json' });
 this.options = new RequestOptions({ headers: this.headers });'/api/todo/create', JSON.stringify(todo), this.options);

I also have an API action as seen below (ASP.NET Core)

public IActionResult Create([FromBody] Todo todo)
    return this.Ok();

This code works but I am not comfortable in seeing the [FromBody] attribute in the parameter. I know that for Post requests, MVC reads it from the body. But, I have been using Restangular in AngularJS before and the parameter object is sent to an API without the [FromBody] attribute.

Is there a way to just pass the object to the API action without using the [FromBody] attribute or reading the content of the request and then deserializing it to an object?


I think using the payload of a POST method is what you should do to send data to the server. You can choose the content type you want (JSON, url encoded form, …).

That said, the Angular2 HTTP support is low-level. I mean there is no high-level API for REST. You need to set headers by your own, choose the HTTP method you want to use… I won’t find something like Restangular in Angular2.

This link could help you:

Answered By – Thierry Templier

Answer Checked By – Mary Flores (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.