undefined //console.log(req.body)
TypeError: Cannot read property 'username' of undefined
at module.exports (/Users/Assa/Development/JsonServer/headers.js:9:40)
at Layer.handle [as handle_request] (/usr/local/lib/node_modules/json-server/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/usr/local/lib/node_modules/json-server/node_modules/express/lib/router/index.js:312:13)
at /usr/local/lib/node_modules/json-server/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/usr/local/lib/node_modules/json-server/node_modules/express/lib/router/index.js:330:12)
at next (/usr/local/lib/node_modules/json-server/node_modules/express/lib/router/index.js:271:10)
at module.exports (/Users/Assa/Development/JsonServer/hello.js:5:3)
at Layer.handle [as handle_request] (/usr/local/lib/node_modules/json-server/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/usr/local/lib/node_modules/json-server/node_modules/express/lib/router/index.js:312:13)
at /usr/local/lib/node_modules/json-server/node_modules/express/lib/router/index.js:280:7
Hi @adn5,
I think t's possible to get an empty body if you don't have Content-Type: application/json. So you may want to check the correct header is set.
Also, if you use the project as a module, you need to load body-parser to be able to do req.body:
https://github.com/typicode/json-server#custom-routes-example
server.use(jsonServer.bodyParser)
server.use('/some/path', function (req, res, next) {
console.log(req.body)
next()
})
If you need further help, feel free to paste some of your code and your db.json :)
It was working before 0.9.0. after updating from 0.8 to 0.9 it stopped working.
I am sure that i am sending application/json as content-type.
These are only resources I am using one is middleware and other is json generator.
Header.js //middleware
module.exports = function (req, res, next) {
var bodyParser = require('body-parser');
var faker = require("faker");
console.log("----------------------------");
console.log(req.body);
console.log("----------------------------");
//console.log(req);
if(req.url === '/login') {
if (!req.body) return res.sendStatus(400);
if (req.method === 'POST' && req.body.username.indexOf('test') > -1 && req.body.password.indexOf('test') > -1) {
res.header('AUTH-TOKEN', faker.random.uuid())
next()
} else {
res.sendStatus(401)
}
}
else {
console.log("Checking X-AUTH-TOKEN:" + req.get('X-AUTH-TOKEN'))
if(req.get('X-AUTH-TOKEN') !== undefined) {
console.log("Found X-AUTH-TOKEN")
next()
}
else {
console.log("X-AUTH-TOKEN NOT FOUND")
res.sendStatus(401)
}
}
}
Generate.js instead of db.json
module.exports = function() {
var faker = require("faker")
faker.locale = "sv";
var _ = require("lodash")
return {
"login":{
}
}
}
I'm having the same issue, can't find req.body:
>>req: _consuming;_dumped;_events;_eventsCount;_maxListeners;_parsedUrl;_readableState;_remoteAddress;_startAt;_startTime;baseUrl;client;complete;connection;domain;headers;httpVersion;httpVersionMajor;httpVersionMinor;method;next;originalUrl;params;query;rawHeaders;rawTrailers;readable;res;route;socket;statusCode;statusMessage;trailers;upgrade;url
(where content-type = application/json)
I've confirmed that I do get req.body on 0.8.23:
>>req: _body;_consuming;_dumped;_events;_eventsCount;_maxListeners;_parsedUrl;_readableState;_remoteAddress;_startAt;_startTime;baseUrl;body;client;complete;c
onnection;domain;headers;httpVersion;httpVersionMajor;httpVersionMinor;length;method;next;originalMethod;originalUrl;params;query;rawHeaders;rawTrailers;read;readabl
e;res;route;socket;statusCode;statusMessage;trailers;upgrade;url
but not on the latest in v0.9.4
I notice a few more default middlewares being loaded in 0.8.23, including jsonParser and urlencodedParser.
Did anyone find anything on that? I'm getting undefined jsonServer.bodyServer when I try to do
server.use(jsonServer.bodyParser)
as suggested in the example
The results is an exception
throw new TypeError('app.use() requires middleware functions');
@danbars
I can't replicate your issue, please give us more details about how you setup json-server, maybe full code.
also, please do json-server --v' in your command line and paste the output here.
Same here, reverted to 0.8.23 to prevent the issue.
Same here, was using latest version, rolled back to 0.8.23 because of missing body in POST request when going through the middleware.
We have been having issues with the req.body being empty on POST requests in our middleware, since version 9.0.0. We use this to transform POST requests into GET, since our applications need this, but we need to parse the body for this.
I've been trying to look into this. The only thing I've found so far is that it broke on this commit:
https://github.com/typicode/json-server/commit/2b26630ac6379fba77eb104b22e83b41a004b52e\
It works on the commit before, but I haven't found the part that breaks this.
The middleware that I'm using to test is the following:
module.exports = function (req, res, next) {
console.log('middleware', req.method);
if(req.method === 'POST') {
console.log('middleware body', req.body);
}
req.method = 'GET';
// Done
next();
};
And since this commit the req.body keeps logging as 'undefined'
I've found the problem and created a pull request with the fix. As soon as this is accepted, I'd expect it to be fixed in the next release.
Great, waiting for the new release! :)
Any update for new release?
The release of v1.12.0 was done on Aug 2.
I am missing body on post requests when content-type in headers is application/vnd.api+json
Please help
missing body on post requests when content-type in headers is application/vnd.api+json
Same thing for me. [email protected]
Most helpful comment
Hi @adn5,
I think t's possible to get an empty body if you don't have
Content-Type: application/json. So you may want to check the correct header is set.Also, if you use the project as a module, you need to load
body-parserto be able to doreq.body:https://github.com/typicode/json-server#custom-routes-example
If you need further help, feel free to paste some of your code and your
db.json:)