1

I have an angularjs app posting over https to a node express server. The angular client is posting with the header:

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

On the server:

var bodyParser = require('body-parser');

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

This produces a request body that looks like:

{ '{"firstname":"hj","lastname":"hj"}': '' }

Without bodyParser I don't get any body at all.

Obviously I'm doing something wrong. Is there a way to get valid json without trying to unscramble the above request body?

2 Answers 2

1

I believe what you're missing is to let bodyParser be aware of JSON.

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

  // Place it below ^ your urlencoded line

  app.use(bodyParser.json()); // <-- this
Sign up to request clarification or add additional context in comments.

2 Comments

No, I tried that and the request body was the same. I did forget to mention in my question that the angular post includes the header 'Content-Type': 'application/x-www-form-urlencoded'
Change angular to application/json, that should do it
0

MarkPieszak is right in his post and in his comment:

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

I somehow didn't think this would work over https

Comments

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.