简体   繁体   中英

cURL JSON Data Node.js

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?

对于 Windows 用户,它可以转义"字符。

curl -X POST -d "{\"user\":\"jane.doe\"}" -H "Content-Type: application/json" http://localhost:3000/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM