Reputation: 13
I have a Problem with this code:
const express = require('express');
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.get('/', (req, res) => {
res.send([
{ id: 1, title: 'foo' }
]);
});
app.post('/', (req, res) => {
res.send({
data: req.body
})
console.log("Geht");
});
app.listen(3000, () => {
console.log('Server listening on port 3000.');
});
If I try to use "curl -X POST -d '{"user":"jane.doe"}' -H "Content-Type: application/json" http://localhost:3000/" for example, I always get the following error:
SyntaxError: Unexpected token u in JSON at position 1 at JSON.parse () at parse (C:\Programmieren\node test\node_modules\body-parser\lib\types\json.js:89:19) at C:\Programmieren\node test\node_modules\body-parser\lib\read.js:121:18 at invokeCallback (C:\Programmieren\node test\node_modules\raw-body\index.js:224:16) at done (C:\Programmieren\node test\node_modules\raw-body\index.js:213:7) at IncomingMessage.onEnd (C:\Programmieren\node test\node_modules\raw-body\index.js:273:7) at IncomingMessage.emit (events.js:327:22) at endReadableNT (internal/streams/readable.js:1327:12) at processTicksAndRejections (internal/process/task_queues.js:80:21)
Can somebody tell me a solution to fix it?
Upvotes: 1
Views: 634
Reputation: 863
For windows users, it worked with escaping the "
character.
curl -X POST -d "{\"user\":\"jane.doe\"}" -H "Content-Type: application/json" http://localhost:3000/
Upvotes: 0