4

I have some pretty vanilla code, but can't seem to figure out the issue. On the server side, I get an empty object in the body of the request when sending the POST below. I'm using express on the server side. My server side works fine when I use Postman, so the server side isn't the issue as far as I can tell. From everything I can find, the code below should work just fine:

$http({
    method: 'POST',
    url: 'http://localhost:5000/authRequest',
    headers : { 'Content-Type' : 'application/json' },
    body: {
        'test' : 'test'
    }
});

Any thoughts?

5
  • try use tyle: "post" Commented Mar 13, 2017 at 13:01
  • "On the server side, I get an empty object in the body of the request" — Where is your server side code? How are you examining the body of the request? Commented Mar 13, 2017 at 13:05
  • Have you used the developer tools in your browser to look at the raw HTTP request? Is it formatted as you expect? Does it use the method you expect? Commented Mar 13, 2017 at 13:07
  • in angular $http there is no such propertybody it looks that you must read the official documentation here docs.angularjs.org/api/ng/service/$http. The correct property is the data one Commented Mar 13, 2017 at 13:08
  • @HoangHieu — I looked at the documentation. I don't see any mention of tyle (or type for that matter). method is correct. Commented Mar 13, 2017 at 13:08

1 Answer 1

5

https://docs.angularjs.org/api/ng/service/$http#post

There is no such parameter as 'body' in post request. You should use 'data':

$http({
              method: 'POST',
              url: 'http://localhost:5000/authRequest',
              headers : { 'Content-Type' : 'application/json' },
              data: {'test' : 'test'}
}).then(function(response){
   // your logic for viewing
});
Sign up to request clarification or add additional context in comments.

1 Comment

Geez. Thanks. Step 0: read the documentation and pay attention. Switched "body" to "data" and it works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.