Express – 使用curl添加JSON负载,导致parsing错误

码:

const express = require('express') const bodyParser = require('body-parser') app = express() app.use(bodyParser.json()); app.post('/', function (req, res) { res.send("Ok") }) app.listen(7000) 

作品:

curl -X POST localhost:7000 /

失败:

Cmd: curl -H“Content-Type:application / json”-d {“day”:“Friday”} localhost:7000 /

错误: SyntaxError:位置1的JSON中的意外标记d

有任何想法吗?

parsing度:

这个问题似乎是由于我在Windows上这样做的缘故。 以下命令工作。

 curl -H "Content-Type: application/json" -d {"""day""":"""Friday"""}localhost:7000/ curl -H "Content-Type: application/json" -d {\"day\":\"Friday\"} localhost:7000/ curl -H "Content-Type: application/json" -d "{\"day\":\"Friday\"}" localhost:7000/ 

cURL命令与引号混淆(看看它认为第一个字符是“d”?)。 您需要将JSON数据换成单引号:

 curl -H "Content-Type: application/json" -d '{"day":"Friday"}' localhost:7000/ 

你也可以逃避引号:

 curl -H "Content-Type: application/json" -d "{\"day\":\"Friday\"}" localhost:7000/