Chris Chris - 5 months ago 17
HTTP Question

How does one format a path in order to take in a generic JSON object in Express.JS?

Below is a generic use of Js, Node.js, and Express.js

app.get('/person', function(req, res, next){

I am sending the current curl command

curl -v -H "Content-Type: application/json" -X GET -d '{"name":"me", "phone":"123"}' localhost:1337/persons

And my console.log() as I assumed would print {} an empty container. Now from reading the Express.js documentation I know I could arrange the URL to take in the parameters: /persons/:person/id/:id and make my JSON input valid, but what I want to know is if I can take any object JSON passed by a user? In other words, what if my json had an extra field; would this require an entirely new express handler? If this is not possible, is this due to security issues or is this just really poor design? Thanks


As man curl says -d or --data adds the data into the body of the request.

-d, --data

(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded. Compare to -F, --form.

-d, --data is the same as --data-ascii. --data-raw is almost the same but does not have a special interpretation of the @ character. To post data purely binary, you should instead use the --data-binary option. To URL-encode the value of a form field you may use --data-urlencode.

So, you would need a package like body-parser to parse it.

install it like npm install body-parser and add these lines to your code and your issue will be solved.

var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: true }));


I'd recommend to use -X POST instead of GET in your curl and in your code if you want to send a body payload.

Also please note typo: the curl is to /persons but the code has app.get('/person', correct either one of them.