将 curl 和 json 与快速服务器一起使用

using curl and json with express server

我想在我的本地快速服务器上使用 curl 和 json 格式的数据测试一些 api,遵循 curl 文档,例如:

curl -v -k https://www.localhost.fr/api/test -H 'Content-Type:application/json' -d '{"test":"test"}'

没有 json 在 express 中解析并公开 headers 是错误的:

''Content-Type'
'application/json''

我删除了 Content-type 中的引号,headers 是正确的,但现在 express.json() 中间件发送错误:

JSON 位置 0

中的意外标记 '

我尝试重写数据:

{"test":"test"}

{test:test}

"test"

"{\'test':\'test\'}"

..etc 我想我已经尝试了所有可以写的方法..

只有 {} 个“{}”不会抛出解析错误

然后我尝试查看在 json 之上使用中间件 raw() 作为数据快递接收的数据,但我没有任何可解析的东西,没有 body,没有数据. (这里是第二个issue/misunderstanding)

我指定在 curl 之外一切正常(使用 fetch 的 React 应用程序)。

curl 7.79.1,nodejs 16,express 4

一些body运行成这个问题? curl 是一个很酷的工具,但我在这里真的很困惑..;(,提前致谢!

在 Windows 上,您必须使用双引号:curl -H "Content-Type:application/json" -d "{\"test\": \"test\"}" ...