我正在关注有关创建基于 javascript 的服务器/客户端环境的在线教程,为了测试 POST 方法,作者提供了一段要运行的 cURL 代码。然而,当我尝试运行它时,出现错误。
我做了一些研究,我相当确定提供的代码适用于 Linux 环境,但我在 Windows 10 上操作。我尝试将 \
字符更改为 ^
但我仍然收到错误。我使用了 cmd 提示符和 PowerShell。
curl -X POST \
http://localhost:3000/signup \
-H 'Content-Type: application/json' \
-d '{
"email": "test5@test.com",
"password": "1234",
"name": "test5"
}'
我希望代码将数据发布到我的数据库,但我得到以下输出:
C:\Users\ricks>curl -X POST \
curl: (6) Could not resolve host: \
C:\Users\ricks> http://localhost:3000/signup \
'http:' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\ricks> -H 'Content-Type: application/json' \
'-H' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\ricks> -d '{
'-d' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\ricks>"email": "test5@test.com",
'"email":' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\ricks>"password": "1234",
'"password":' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\ricks>"name": "test5"
'"name":' is not recognized as an internal or external command,
operable program or batch file.
最佳答案
虽然提供的所有答案无疑将使我能够运行上面发布的 cURL 代码,但我找到了仅使用 Windows cmd 提示符的解决方法。
首先,我将代码编译为单行而不是多行。其次,我发现编译错误主要来自未转义的 "
字符。最终,以下代码正常工作并将数据 POST 到我的数据库!
curl -X POST http://localhost:3000/signup -H "Content-Type:application/json" -d "{\"email\" : \"test4@test.com\", \"password\" : \"1234\", \"name\": \"test5\" }"
虽然这可能不是最可持续的方法,但这对我来说是一个很好的学习时刻,它可能会帮助那些希望利用一次性 cURL 执行而不需要向系统下载任何额外内容的人。
关于javascript - 如何在 Windows 10 上编译 cURL 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57450726/