繁体   English   中英

cURL JSON 数据 Node.js

[英]cURL JSON Data Node.js

我有这个代码的问题:


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.');
});

如果我尝试使用 "curl -X POST -d '{"user":"jane.doe"}' -H "Content-Type: application/json" http://localhost:3000/" 例如,我总是得到以下错误:

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) 在完成 (C:\\ Programmieren\\node test\\node_modules\\raw-body\\index.js:213:7) 在 IncomingMessage.onEnd (C:\\Programmieren\\node test\\node_modules\\raw-body\\index.js:273:7) 在 IncomingMessage.emit (events.js:327:22) at endReadableNT (internal/streams/readable.js:1327:12) at processTicksAndRejections (internal/process/task_queues.js:80:21)

有人可以告诉我解决它的解决方案吗?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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