问题
I have a simple cURL (i think this is right) that posts a small JSON object to my express server:
curl -d "{'test': 'this'}" localhost:3000/rest/user/authenticate
I have express set up as:
// set up body parsing in express to be able to get parse JSON posts
server.use(express.json());
server.use(express.urlencoded());
and have handler that accepts the route:
JSON = require('JSON')
module.exports = {
authenticateUser: function create(req, res){
var postedObject = req.body
console.log(postedObject)
res.send('Handle Post: authenticateUser');
}
}
the handler is getting called, but it is logging the JSON body unexpectedly:
{ '{\'test\': \'this\'}': '' }
So my entire object looks to be the name side of a JSON Name:Value pair object. no matter what I post it seems to be appending the value side. Unless I do something like this:
curl -d "a=a" localhost:3000/rest/user/authenticate
which logs:
{'a':'a'}
so have i not set the right headers? Configured express wrong? I plan on digging through the express code, but wondered if somebody might know before I find the solution. Either way having a searchable/indexed answer to this on the web will be nice.
update 1
ok I need to add the header to the cURL
curl -H "Content-Type: application/json" -d "{'test': 'this'}" localhost:3000/rest/user/authenticate
which gives the error:
Parsing: {'test': 'this'}
SyntaxError: Unexpected token '
at Object.parse (native)
at C:\blah\node_modules\express\node_modules\connect\lib\middleware\json.js:86:19
at IncomingMessage.onEnd (C:blah\node_modules\express\node_modules\connect\node_modules\raw-body\index.js:109:7)
at IncomingMessage.g (events.js:180:16)
at IncomingMessage.EventEmitter.emit (events.js:92:17)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)
OR curl -H "Content-Type: application/json" -d "{test: 'this'}" localhost:3000/rest/user/authenticate
which gives the error:
Parsing: {test: 'this'}
SyntaxError: Unexpected token t
at Object.parse (native)
at C:\blah\node_modules\express\node_modules\connect\lib\middleware\json.js:86:19
at IncomingMessage.onEnd (C:blah\node_modules\express\node_modules\connect\node_modules\raw-body\index.js:109:7)
at IncomingMessage.g (events.js:180:16)
at IncomingMessage.EventEmitter.emit (events.js:92:17)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)
update 2
in the file connect/lib/middleware/json.js
this line seems to be the one causing issues
req.body = JSON.parse(buf, options.reviver);
update 3
I really think it is my cURL
buf= JSON.stringify({test: 'This'});
console.log(buf)
req.body = JSON.parse(buf, options.reviver);
works logging first {"test":"this"}
and then in my handler:
----------------------
{ test: 'this' }
----------------------
回答1:
1) JSON middleware only works if the request has Content-Type: application/json
header.
2) Valid JSON should contain "
, not '
.
So it should be '{"test": "this"}'
instead of "{'test': 'this'}"
Try this command:
curl -d '{"test": "this"}' -H "Content-Type: application/json" localhost:3000/rest/user/authenticate
回答2:
THe answer has two pieces
- add the content header
on windows go through the pain of passing the json correctly, cURL on my windows machine doesn't deal with interchanging the single and double quotes , therefore the proper cURL is
curl -H "Content-Type: application/json" -d "{"""test""": """this"""}" localhost:3000/rest/user/authenticate
Yup, inside of the data parameter, you need to use three double quotes to send a single double quote to the server.
Accepting zub's answer, because he is correct. I was trying a bunch of things to get around windows here.
回答3:
For those who use postman like me just try to send the request as POST>>RAW and create your Json to do the post, in my test i have created a simple json like this and works:
{ "name": "test" }
Using form-data or x-www-form-urlencoded it doesn't work.
回答4:
Under Windows cmd:
curl -H "Content-Type: application/json" -d "{"""test""": """this"""}" localhost:3000/rest/user/authenticate
or use cygwin:
curl -H "Content-Type: application/json" -d '{"test": "this"}' localhost:3000/rest/user/authenticate
My 2 cents
来源:https://stackoverflow.com/questions/21661047/node-js-express-json-middleware-not-parsing-request-as-expected