【问题标题】:cURL JSON Data Node.jscURL JSON 数据 Node.js
【发布时间】:2021-10-01 18:21:00
【问题描述】:

我对这段代码有疑问:


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: 位置 1 处 JSON 中的意外标记 u 在 JSON.parse() 解析时(C:\Programmieren\node test\node_modules\body-parser\lib\types\json.js:89:19) 在 C:\Programmieren\node test\node_modules\body-parser\lib\read.js:121:18 在 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) 在 endReadableNT (internal/streams/readable.js:1327:12) 在 processTicksAndRejections (internal/process/task_queues.js:80:21)

谁能告诉我解决办法?

【问题讨论】:

    标签: javascript node.js curl cmd


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2017-07-07
      • 2021-07-16
      • 2012-03-26
      • 2018-02-11
      • 2021-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-03
      相关资源
      最近更新 更多