1

I have this code in my node backend server:

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

And this is the api i.e. i want to call:

router.post('/update/:_id', function(req, res, next) {
    console.log("req.body:" + req.body);
}

This is my post request through angular 1:

var data = {something: "something"}
var url = 'http://localhost:8081/update/5982168b399ccf32ad75ce2e';

$http({
    withCredentials: false,
    method: 'POST',
    url: url,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    data: data
})
.then(function (response) {
    if (response.data) {
        console.log("Post Data Submitted Successfully!");
    }
}, function (response) {
    console.log("Error status: " + response.status)
});

I am using Angular 1.6.1 version.

I have tried everything and looked through countless posts but none helped me. I can see the body going through. But i got undefined req.body in the node side backend.

Any help would be appreciated.

Thanks.

6
  • Change content type to application/json Commented Aug 3, 2017 at 17:06
  • Show you app.js (where you calling app.use and routes). Commented Aug 3, 2017 at 17:13
  • Welcome to Stack Overflow! Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a minimal reproducible example. Use the "edit" link to improve your question - do not add more information via comments. Thanks! Commented Aug 3, 2017 at 17:16
  • Already tried the change to application/json and did not work. Commented Aug 3, 2017 at 17:24
  • The routes are ok. I can see it going to the api. I can see the log, it just says undefined instead of having something inside. Commented Aug 3, 2017 at 17:25

2 Answers 2

3

it could be the

headers: {'Content-Type': 'application/x-www-form-urlencoded'}

your data is in JSON format, not x-www-form-urlencoded

{something: "something"}

you could try this

headers: {'Content-Type': 'application/json'}

by default you send JSON data in $http POST, so you may not need the header just take it out and try

===============================================================

also, your data may not be in valid format JS does not know what something is, it's not a variable right? you have to make it a string, even if its the key of a { "key": "value" } pair you could try this if above doesn't work

{
    "something": "something"
}

to verify your JSON https://jsonlint.com/

Sign up to request clarification or add additional context in comments.

Comments

0

You need to stringify the data, and change the content type as was already said:

var data = {something: "something"}
var url = 'http://localhost:8081/update/5982168b399ccf32ad75ce2e';

$http({
    withCredentials: false,
    method: 'POST',
    url: url,
    headers: {'Content-Type': 'application/json'},
    data: JSON.stringify(data)
})

You should in addition make sure that you can make a POST to that url using Postman and that the body is received to discart a problem in the server. On a side note, consider using Angular Resource.

1 Comment

$http does this by default internally

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.