How can I POST data to a WebAPI controller from Angular's $http service?

19,243

Solution 1

Adding public to the property on MyModel fixed it (facepalm).

I ended up using this on the client:

var data = { ID: 1 };
$http.post('/api/MyResource/MyPostAction', data);

Thanks everyone.

Solution 2

You don't need to wrap your data into model property:

var data = { ID: 1 };
$http.post('/api/MyResource/MyPostAction', data);

Solution 3

$http.post('/api/MyResource/MyPostAction', data);

Remove the JSON.stringify and post the data as is.

You don't need to specify json header either as it is the default for post.

On the server:

Remove Xml from webapi if you're only using json

//WebApiConfig.js
var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
Share:
19,243
Jesus is Lord
Author by

Jesus is Lord

1 Corinthians 15:3-4 For I delivered to you first of all that which I also received: that Christ died for our sins according to the Scriptures, and that He was buried, and that He rose again the third day according to the Scriptures Romans 10:9 that if you confess with your mouth the Lord Jesus and believe in your heart that God has raised Him from the dead, you will be saved Philippians 2:10 that at the name of Jesus every knee should bow, of those in heaven, and of those on earth, and of those under the earth

Updated on June 14, 2022

Comments

  • Jesus is Lord
    Jesus is Lord almost 2 years

    I tried to follow this example.

    Here is my C# code:

    public class MyModel
    {
        int? ID { get; set; }
    }
    
    public class MyResourceController : ApiController
    {
        [HttpPost]
        public MyModel MyPostAction(MyModel model)
        {
            return model;
        }
    }
    

    Here is my JavaScript:

    var data = { model: { ID: 1 } };
    $http.post(
        '/api/MyResource/MyPostAction',
        JSON.stringify(data),
        {
            headers: {
                'Content-Type': 'application/json'
            }
        }
    );
    

    When I set a breakpoint in my action and model.ID is null instead of 1. How can I POST a complex object?

  • Jesus is Lord
    Jesus is Lord over 10 years
    That didn't work. :/ (I took out the header config object and I took out the JSON.stringify)
  • Jesus is Lord
    Jesus is Lord over 10 years
    Oh I've used the Package Manager to get Ninject and Newtonsoft's JSON.NET. Maybe that's interfering somehow?
  • Steve Klösters
    Steve Klösters over 10 years
    JSON is only the default if data is supplied and not undefined. Otherwise it is XML. This is important if you don't have a media formatter for XML for your Web API controller.
  • James Kyburz
    James Kyburz over 10 years
    @WordsLikeJared Strange. Have updated the answer to remove the xml formatter from the webapi, although it shouldn't be that stupid to try xml when the http header states json
  • Jesus is Lord
    Jesus is Lord over 10 years
    I was missing public on my property... This helped though. Thanks.
  • Jesus is Lord
    Jesus is Lord over 10 years
    I was missing public on my property. Thanks for all the help though.