Question

Not sure what happened, but req.body parameters are not showing up when I reach my controllers in a POST request.

I have turned off csrf in the middleware.json:

{
    "middleware": {
        "errorPages": {
            "404": "errors/404",
            "500": "errors/500",
            "503": "errors/503"
        },

        "session": {
            "module": "connect-redis",
            "config": {
                "ttl": 3600
            },
            "secret": "<some secret>"
        },
        "appsec": {
            "csrf": false,
            "csp": false,
            "p3p": false,
            "xframe": "SAMEORIGIN"
        }
//
//      "compiler": {
//          "dust": "templates",
//          "less": "css"
//      },
//


//      "static": {
//          "srcRoot": "path:./public",
//          "rootPath": "path:./.build"
//      }
    }
}

The controller code:

app.post('/user', function (req, res) {
        res.format({
            json: function () {
                //extract the user from the req
                try {
                    var user = new User();
                    user.firstName = req.body.firstName;
                    user.lastName = req.body.lastName;
                    user.userName = req.body.userName;
                    user.password = req.body.password;
                    user.email = req.body.email;

                    //subsequent code omitted...
                } catch (err) {
                    //handle error
                }
            }
        })
    });

The POST request looks like this:

POST /user HTTP/1.1 Host: localhost:8000 Cache-Control: no-cache

{ "firstName":"John", "lastName" : "Doe", "userName": "jdoe", "password" : "somepassword", "email": "jdoe@jdoe.com" }

Was it helpful?

Solution

You have to set the headers in this instance to Content-Type application/json

POST /user HTTP/1.1
Host: localhost:8000
Content-Type: application/json
Cache-Control: no-cache

{ "firstName":"John", "lastName" : "Doe", "userName": "jdoe", "password" : "somepassword", "email": "jdoe@jdoe.com" }

OTHER TIPS

What binarygiant said. If you're unable to set headers on your request, you can always serialize your JSON before posting it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top