Angular POST request received in NET Core Api as Null

angular 6 post data to web api
how to post data from angular to web api
c# web api post request body
web api post receiving null
web api post method parameter is null
angular http post asp net core
web api post json frombody
net core web api post string

Im POSTing some data via Angular 6, but my Core API keeps returning nulls:

Request:

{"id":0,"name":"test","weight":2,"frequency":2,"activityTypeModelId":3}

Response:

{id: 0, name: null, weight: 0, frequency: 0, activityTypeModelId: 0}

Controller:

[HttpPost("[action]")]
public IActionResult Add([FromForm]Model model)
{
    return new JsonResult(model);
}

Angular, using HttpClient:

add(Model: model) {
     return this.http.post(this.addUrl, model);
}

API Model:

public class Model
{
    [Key]
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public int Weight { get; set; }
    public int Frequency { get; set; }
    public int ActivityTypeModelId { get; set; }
}

TS Model:

 export class Model{
   id?: number;
   name?: string;
   weight?: number;
   frequency?: number;
   activityTypeModelId?: number;
 }

Everything works fine when I'm using Postman. I already tried with [FromBody]. Where is the problem?

I dont know why, but this fixed my issue:

I created a header:

 const header = new HttpHeaders()
     .set('Content-type', 'application/json');

Changed the POST function by adding a header and JSON.Stringyfy the object:

 add(model: Model): Observable<Model> {
     const body = JSON.stringify(c);
     return this.http.post<Model>(this.addUrl, body, { headers: header} );
   }

Changed [FromForm] to [FromBody].

Adding JSON.stringify(model) in the parameters of the http.post was not working.

JSON that is working with the CORE Api:

{"name":"test","weight":2,"activityTypeModelId":15}

JSON that is not working with the CORE Api:

{name:"test",weight:2,activityTypeModelId:15}

Without the header I encountered a 415 error from the API.

ASP.NET CORE Web API: Value are always null when doing HTTP , So here's my scenario: I'm trying to do a HTTP Post request to an api NET CORE Web API: Value are always null when doing HTTP Post How come that it can't receive the values being passed from postman to the api  ASP.NET Core Web API – Post, Put, Delete. In the previous post, we have created a GET request with some code refactoring actions. In this post, we are going to create POST PUT DELETE requests and by doing so we are going to complete the server part (.NET Core part) of this series.

Try

return this.http.post(this.addUrl, JSON.stringify(model) );

CRUD Operation With Angular 5 HTTPClient and ASP.NET Core , NET Core Web API into that app using HTTPClient. is the toolkit which enables us to send and receive data over RESTful HTTP endpoints. ASP.NET CORE Web API: Value are always null when doing HTTP Post requests. #2202. lorenz31 opened this issue on Sep 18, 2017 · 46 comments. lorenz31 commented on Sep 18, 2017 • I'm experiencing a problem doing post requests via ASP.NET Core Web API. I'm a bit confused of the articles I read/found while Googling. So here's my scenario:

I think that, in .NET Core 2.1 is (see https://docs.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-2.1)

HttpPost("[action]")]
//see that I put [FromBody]
public IActionResult Add([FromBody]Model model)
{
    //OK is one of several IActionResult 
    return OK(model);
}

Post method returns null values in web api using angular 4 , Free source code and tutorials for Software developers and Architects.; Updated: 8 Jun 2017. HttpClient POST, PUT, and DELETE Request. To send data to the REST API is really simple with Angular HttpClient. Still, in the same API service file, add a function to POST data to the REST API.

I had this issue and the problem was that I was trying to bind to an interface:

[HttpPost("[action]")]
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK]
public bool SaveResponse([FromBody]ISaveRequestViewModel request) =>
    Service.SaveResponse(request.Responses);

I fixed it by changing it to a class:

[HttpPost("[action]")]
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK]
public bool SaveResponse([FromBody]SaveRequestViewModel request) =>
    Service.SaveResponse(request.Responses);

This model also had to use classes instead of interfaces:

public class SaveRequestViewModel
{
    public List<IQuestionResponseViewModel> Responses { get; set; }
}

Became

public class SaveRequestViewModel
{
    public List<QuestionResponseViewModel> Responses { get; set; }
}

I guess the model binder just doesn't work with interfaces.

CRUD Operation With Angular 5 HTTP Client And ASP.NET Core , In Angular 4.3, this Angular HTTP API was provided which in the use to map the Data which we are sending and receiving from the API code  Angular POST request received in NET Core Api as Null. but my Core API keeps returning nulls: Request: Angular-Web api post method of model is null. 1.

Accepting Raw Request Body Content in ASP.NET Core API , ASP.NET • C# • HTML5 • JavaScript • AngularJs NET Core Web API project and changed the default ValuesController to this sample You can accept a string parameter and post JSON data from the client pretty easily. request, Encoding encoding = null) { if (encoding == null) encoding = Encoding. Re: Angular 2 http.post not posting Jan 21, 2016 03:32 AM|Navin Dhananshan|LINK. In Http.post do the following. JSON.stringify({item:item}); in this the first item is controller action parameter name and second item is the variable which holds value. open the browser console and see how the query param is sent.

Consume Web API in .NET using HttpClient, .NET 4.5 includes HttpClient class to overcome the limitation of WebClient. class in console application to send data to and receive data from Web API which is Net; using System.Net.Http; using System.Web.Http; namespace MyWebAPI. includeAddress = false) { IList<StudentViewModel> students = null; using (var ctx  But when using HttpClient.post from Angular, the body data received in the server-side are ugly and as described before in this issue. So..The only thing I can confirm now is that it works well with POSTMAN but not with HttpClient

Implementing GET Method in Web API, Learn how to implement Get method which handles HTTP GET request in ASP.​NET Web API here. action methods in our Web API controller class that will handle HTTP GET requests. StudentAddress == null ? null : new AddressViewModel() { StudentId = s. Net, JavaScript, jQuery, AngularJS, Node.​js to your inbox. The ASP.NET server side code (in this case it is Action method of the controller of ASP.NET MVC project) simply uses Request.Form["keyname"] to retrieve data sent from the AngularJS and returning it as ContentResult.

Comments
  • Your action is returning something named newCircumstance, what is that?
  • What do you see in the network tab of developer console in your browser? Are you able to see request body over there?
  • When you break in the C# Action is Model populated? When you look at Browser Dev tools what is getting submitted?
  • @R.Richards , i forgot to change that name here after pasting. I changed it in the edit.
  • @AmitChigadani No, there is no requestbody.