gpt4 book ai didi

node.js - curl 语法错误 : Unexpected token ' in JSON at position 0

转载 作者:行者123 更新时间:2023-12-02 18:44:08 31 4
gpt4 key购买 nike

当我执行这个请求时 curl -X POST -H "Content-Type:application/json"http://localhost:3000/messages -d '{"text":"Hi again, World"} ' 在 cURL 上到 express 我得到这个错误

    <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>SyntaxError: Unexpected token &#39; in JSON at position 0<br> &nbsp; &nbsp;at JSON.parse (&lt;anonymous&gt;)<br> &nbsp; &nbsp;at createStrictSyntaxError (C:\Users\Atom\node_modules\body-parser\lib\types\json.js:158:10)<br> &nbsp; &nbsp;at parse (C:\Users\Atom\node_modules\body-parser\lib\types\json.js:83:15)<br> &nbsp; &nbsp;at C:\Users\Atom\node_modules\body-parser\lib\read.js:121:18<br> &nbsp; &nbsp;at invokeCallback (C:\Users\Atom\node_modules\raw-body\index.js:224:16)<br> &nbsp; &nbsp;at done (C:\Users\Atom\node_modules\raw-body\index.js:213:7)<br> &nbsp; &nbsp;at IncomingMessage.onEnd (C:\Users\Atom\node_modules\raw-body\index.js:273:7)<br> &nbsp; &nbsp;at IncomingMessage.emit (events.js:327:22)<br> &nbsp; &nbsp;at endReadableNT (internal/streams/readable.js:1327:12)<br> &nbsp; &nbsp;at processTicksAndRejections (internal/process/task_queues.js:80:21)</pre>
</body>
</html>

我不知道我做错了什么,因为我只是从 this 复制粘贴内容教程。我进行了搜索,但在 curl 请求中找不到任何语法错误,这仍然是错误的原因,我知道 ' 是单引号 (')。任何帮助将不胜感激。我的 app.js 文件:

var express = require('express');
var app = express();
const { v4: uuidv4 } = require('uuid');
require('dotenv').config();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

let users = {
1: {
id: '1',
username: 'Robin Wieruch',
},
2: {
id: '2',
username: 'Dave Davids',
},
};

let messages = {
1: {
id: '1',
text: 'Hello World',
userId: '1',
},
2: {
id: '2',
text: 'By World',
userId: '2',
},
};


app.get('/users', (req, res) => {
return res.send(Object.values(users));
});

app.get('/users/:userId', (req, res) => {
return res.send(users[req.params.userId]);
});

app.post('/messages', (req, res) => {
const id = uuidv4();
const message = {
id,
text: req.body.text
};

messages[id] = message;

return res.send(message);
});

app.get('/messages', (req, res) => {
return res.send(Object.values(messages));
});

app.get('/messages/:messageId', (req, res) => {
return res.send(messages[req.params.messageId]);
});


app.post('/users', (req, res) => {
return res.send('POST HTTP method on user resource');
});

app.put('/users/:userId', (req, res) => {
return res.send(
`PUT HTTP method on user/${req.params.userId} resource`,
);
});

app.delete('/users/:userId', (req, res) => {
return res.send(
`DELETE HTTP method on user/${req.params.userId} resource`,
);
});

app.listen(process.env.PORT, () =>
console.log(`Example app listening on port ${process.env.PORT}!`),
);

最佳答案

进一步调查表明,如果您使用的是 Windows cmd 终端,您可能需要转义双引号并避免使用单引号,因此例如在 git bash 上可以执行以下操作

curl -X POST -H "Content-Type:application/json" -d '{"route": "Test route"}' http://192.168.1.xx:xxxx/

但不是在 Windows cmd 上。为此,我使用了:

curl -X POST -H "Content-Type:application/json" -d "{\"route\": \"Test route\"}" http://192.168.1.xx:xxxx/

注意把xx xxxx改成你自己的url、端口

此见解的来源是 https://github.com/biofects/Google-Home-Messages/issues/3

戈登

关于node.js - curl 语法错误 : Unexpected token &#39; in JSON at position 0<br>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67684275/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com