gpt4 book ai didi

javascript - Express - 使用curl 添加 JSON 有效负载导致解析错误

转载 作者:太空宇宙 更新时间:2023-11-04 02:00:51 25 4
gpt4 key购买 nike

代码:

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 本地主机:7000/

失败:

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

错误:语法错误:JSON 中位置 1 出现意外的标记 d

有什么想法吗?

分辨率:

问题似乎是由于我在 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/

关于javascript - Express - 使用curl 添加 JSON 有效负载导致解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46009413/

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