Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here’s a brief (as I can make it) description of my problem, along with all relevant code. I'm hoping the wording for this post will be a bit clearer than my previous request for assistance.

I have a .NET Web API, and an AngularJS front end. I have a very simple POST method which accepts a parameter of the ‘Envelope’ type, shown here:

public class Envelope {
    public int listingId { get; set; }
    public string Description { get; set; }

    public override string ToString() {
        return listingId.ToString() + "; " + Description;
    }
}

The actual POST method on the API appears here:

[EnableCors(origins: "http://simpleapiearl.azurewebsites.net", headers: "*", methods: "*")]
public class EnvelopesController : ApiController {
        // POST: api/Envelopes
        public string Post(Envelope env) {
            return "rval: " + env.ToString() + " (and my addition to env)";
        }
    }

My front-end AngularJS $http POST looks like this:

        $scope.testPOST = function () {
            var env = {
                listingId:1234,
                Description:"some desc"
            };

            $http({
                method: 'POST',
                url: 'http://simpleApiEarl.azurewebsites.net/api/envelopes',
                data: JSON.stringify(env),
                headers: {
                    'Content-Type': 'application/json'
                }
            }).
            success(function (data, status, headers, config) {
                $scope.postStatus = 'success: ' + data;
            }).
            error(function (data, status, headers, config) {
                $scope.postStatus = 'error: ' + status;
            });
        }

Here are my issues (numbered for easier reference):

  1. Using all the code as shown above, I get a “400 (Bad Request)” when I call “testPOST()” from my page. This sounds like a .NET Web API routing issue, but I can’t figure out what it is.
  2. I can avoid the 400 (and in fact get a 200) if I change the ‘Content-Type’ header to ‘application/x-www-form-urlencoded’. HOWEVER, that results in the API seeing the ‘env’ parameter as NULL.
  3. I tend to adorn my action method parameter with a ‘[FromBody]’ attribute, but doing so does not fix the problem of ‘env’ being NULL in my API action method.

I have created a simple plunk with my very simple HTML page used to call the API. It can be found here:

http://plnkr.co/edit/hY2OUeg9CRQ1QOz8MGU8?p=info

Thanks very much for any assistance you can provide.

share|improve this question

1 Answer 1

I found a great blog post that supplied a solution to my issue:

http://victorblog.com/2012/12/20/make-angularjs-http-service-behave-like-jquery-ajax/

Thanks to everyone for considering my request!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.