curl json data node.js

jgwigjjp  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(356)

我对此代码有问题:

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-xpost-d'{“user”:“jane.doe”}'-h”内容类型:application/jsonhttp://localhost:3000/“例如,我总是遇到以下错误:
syntaxerror:invokecallback(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(c:\programmieren\node test\node\modules\raw body\index.js:224:16)的json(json:\programmieren\node:19)的json中json中json位置1处json中出现意外的标记u在incomingmessage.oned(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)在process和rejections(internal/process/task_queues.js:80:21)在incomingmessage.oned.oned.oned.onent(c:\programmier/streams\node\u modules:27
有人能告诉我一个解决办法吗?

fivyi3re

fivyi3re1#

对于windows用户,它使用的是转义 " 性格

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

相关问题