2014-09-20 65 views
0

我正在尝试使Express REST API正常工作,并且出于某种原因,JSON正文总是以不必要的引号结尾。我使用下面的配置,快递中间件:Express req.body用引号

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

而且现在用

console.log(JSON.stringify(req.body)); 

为了测试这一点。随着请求主体

{"name": "New Event", "description": "Hello. This is event"} 

我得到

{"{\"name\": \"New Event Thingy\", \"description\": \"Hello. This is event\"}":""} 

,并用身体

"name": "New Event", "description": "Hello. This is event" 

我得到

{"\"name\": \"New Event Thingy\", \"description\": \"Hello. This is event\"":""} 

为什么会出现这些不必要的字符?

回答

2

您的请求可能有错误Content-Type设置。仔细检查您的请求标头中是否设置了Content-Type: application/json

+0

修复它。谢谢! – aftrumpet 2014-09-20 02:43:00