gpt4 book ai didi

json - Node.js JSON 转义问题

转载 作者:太空宇宙 更新时间:2023-11-03 23:10:57 24 4
gpt4 key购买 nike

我一直在关注这个tutorial关于使用 Node.js 和 MongoDB 创建一个简单的 API,但是我在制作 JSON 帖子的格式时遇到了一些问题。

这篇文章的内容大致如下:

curl POST -d '{"test":"test"}' http://127.0.0.1:3000/add

但是,一旦进入 MongoDB,它看起来如下所示:

{\"test\":\"test\"}":"","_id":"4ff725d6349fdf9c0d000004"}

我的POST函数如下:

app.post('/add', function(req, res){
require('mongodb').connect(mongourl, function(err, conn){
conn.collection('docs', function(err, coll){
coll.insert( req.body, {safe:true}, function(err){
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
});
res.end(JSON.stringify(req.body));
});
});
});
});

任何有关如何确保正确处理 POST 的想法都会很有帮助!

提前致谢!

最佳答案

看起来它可能正在将您的 POST 数据解析为表单编码。尝试这个 CURL 命令:

curl -H "Content-Type: application/json" -X POST -d '{"test":"test"}' http://127.0.0.1:3000/add

关于json - Node.js JSON 转义问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11367363/

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