body-parser 出现Unexpected token u in JSON at position 1 错误

发布于 2020-02-24 00:33:51

我通过body-parser进行post请求,index.js 如下:

var express = require('express');
var app = express();
var bodyParser  = require('body-parser');

app.use(bodyParser.urlencoded({
  extended: true
}));
app.use(bodyParser.json());
app.use(express.json());

app.post('/', function(request, response){
    console.log(request.body);
    res.send("test");
});

app.get('/', function(req, res) {
  res.send('Hello World!');
});



app.listen(8080, function() {
  console.log('Example app listening on port 8080!');
});

重启docker没有问题,但运行命令是却出现问题:

curl -d {"user":"Someone"} -H "Content-Type: application/json" --url http://localhost:8080

报错:

Unexpected token u in JSON at position 1
at JSON.parse (<anonymous>)

请问怎么处理?

查看更多

关注者
0
被浏览
674
1 个回答
Rashomon
Rashomon 2020-02-24
这家伙很懒,什么也没写!

问题可能是因为请求的参数,应该设置为字符串。如:

curl -X POST -H "Content-Type: application/json" -d '{"message": "foo"}' http://localhost:8080/messages

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览