gpt4 book ai didi

windows - Windows 中的 curl 命令

转载 作者:可可西里 更新时间:2023-11-01 11:49:20 25 4
gpt4 key购买 nike

我正在按照以下链接学习 feathersjs

http://feathersjs.com/quick-start/

我需要运行以下命令并监视 http://localhost:3000/todo 处的输出

curl 'http://localhost:3000/todos/' -H 'Content-Type: application/json' --data-binary '{ "text": "Do something" }'

当我尝试在 cmd 上运行以下命令时,它显示在 cmd 提示符下无法识别“curl”。

如果我尝试使用 git-bash.exe、bash.exe 或 sh.exe(在 Git/bin 或 shell.w32-ix86 中)、Cygwin.bat(在 cygwin64 中)运行它,它将运行良好并显示结果在浏览器中。

[{"text":"Do something","id":0}]

但是如果尝试通过将它包含到我的路径“C:\\usr\bin"就可以了...

curl: (1) Protocol "'http" not supported or disabled in libcurl
curl: (6) Couldn't resolve host 'application'
curl: (6) Couldn't resolve host text'
curl: (6) Couldn't resolve host 'Do something'
curl: (3) [globbing] unmatched close brace/bracket in column 1

我设法用下面的命令消除了错误

curl "http://localhost:3000/todos/" -H "Content-Type: 'application/json'" --data-binary "{ 'text': 'Do something' }"

但是生成的输出将缺少 Json 对象“文本”...

[{"text":"Do something","id":0},{"id":1}]

问题:
1)修改命令后,Json对象没有解析成功。语法有问题吗?
2)如果没有语法问题,是不是意味着curl需要按照原来的尝试在Unix环境下运行,不能直接在cmd中运行,但是在bash、cygwin等中可以正常运行?
3)C:\Program Files\Git\usr\bin中的curl.exe和C:\Program Files\Git\mingw64\bin中有curl.exe有什么区别?

更新:

Not OK Raw Cap 输出http.content_type == "'application/json'"

OK Raw Cap 输出http.content_type == "application/json"

更新2:

在第二个命令中删除 application/json 中的单引号...显示错误

C:\Users\testUser>"C:\Program Files\Git\mingw64\bin\curl.exe" "http://localhost:3000/todos/" -H "Content-Type: 'application/json'" --data-binary "{ 'text': 'Do something' }"
{"id":1}
C:\Users\testUser>"C:\Program Files\Git\mingw64\bin\curl.exe" "http://localhost:3000/todos/" -H "Content-Type: application/json" --data-binary "{ 'text': 'Do something' }"
SyntaxError: Unexpected token &#39;<br> &nbsp; &nbsp;at Object.parse (native)<br> &nbsp; &nbsp;at parse (C:\Users\testUser\Documents\Framework\Javascript\featherstestNewService\node_modules\body-parser\lib\types\json.js:88:17)<br> &nbsp; &nbsp;at C:\Users\testUser\Documents\Framework\Javascript\featherstestNewService\node_modules\body-parser\lib\read.js:116:18<br> &nbsp; &nbsp;at invokeCallback (C:\Users\testUser\Documents\Framework\Javascript\featherstestNewService\node_modules\body-parser\node_modules\raw-body\index.js:262:16)<br> &nbsp; &nbsp;at done (C:\Users\testUser\Documents\Framework\Javascript\featherstestNewService\node_modules\body-parser\node_modules\raw-body\index.js:251:7)<br> &nbsp; &nbsp;at IncomingMessage.onEnd (C:\Users\testUser\Documents\Framework\Javascript\featherstestNewService\node_modules\body-parser\node_modules\raw-body\index.js:308:7)<br> &nbsp; &nbsp;at emitNone (events.js:67:13)<br> &nbsp; &nbsp;at IncomingMessage.emit (events.js:166:7)<br> &nbsp; &nbsp;at endReadableNT (_stream_readable.js:905:12)<br> &nbsp; &nbsp;at doNTCallback2 (node.js:441:9)

更新 3:

试图替换 curl.exe 使用的 libcurl-4.dll。从“http://curl.haxx.se/download.html”下载 libcurl,从“http://sourceforge.net/projects/mingw/files/MSYS/”下载 MingW32,并将“C:\MinGW\bin”添加到 PATH。然后 grep -rl "libcurl.dll"。 | xargs sed -i 's/libcurl.dll/libcurl-4.dll/g' 按照建议“http://curl.haxx.se/mail/lib-2010-11/0174.html”创建 libcurl-4.dll。然后执行./buildconfig,make,make install。然后将libcurl-4.dll复制到C:\Program Files\Git\mingw64\bin文件夹下,结果还是一样...

更新 4

更改 curl.exe 的来源但仍使用相同的命令,在 mingw64 版本上显示错误。我怀疑 mingw64 curl 需要特殊转义才能使其工作?

C:\Users\testUser\Documents\Framework\Javascript\featherstestNewService>"C:\Program Files\Git\mingw64\bin\curl.exe" 'http://localhost:3000/todos/' -H 'Content-Type: application/json' --data-binary '{ "text": "Do something" }'
curl: (1) Protocol "'http" not supported or disabled in libcurl
curl: (6) Couldn't resolve host 'application'
curl: (6) Couldn't resolve host 'text'
curl: (6) Couldn't resolve host 'Do something'
curl: (3) [globbing] unmatched close brace/bracket in column 1

C:\Users\testUser\Documents\Framework\Javascript\featherstestNewService>"C:\Program Files\Git\usr\bin\curl.exe" 'http://localhost:3000/todos/' -H 'Content-Type: application/json' --data-binary '{ "text": "Do something" }'
{"text":"Do something","id":38}

更新 5

来自手册... curl --manual

  -d, --data <data>
(HTTP) Sends the specified data in a POST request to the HTTP
server, in the same way that a browser does when a user has
filled in an HTML form and presses the submit button. This will
cause curl to pass the data to the server using the content-type
application/x-www-form-urlencoded. Compare to -F, --form.

-d, --data is the same as --data-ascii. --data-raw is almost the
same but does not have a special interpretation of the @ charac-
ter. To post data purely binary, you should instead use the
--data-binary option. To URL-encode the value of a form field
you may use --data-urlencode.

If any of these options is used more than once on the same com-
mand line, the data pieces specified will be merged together
with a separating &-symbol. Thus, using '-d name=daniel -d
skill=lousy' would generate a post chunk that looks like
'name=daniel&skill=lousy'.

If you start the data with the letter @, the rest should be a
file name to read the data from, or - if you want curl to read
the data from stdin. Multiple files can also be specified. Post-
ing data from a file named 'foobar' would thus be done with
--data @foobar. When --data is told to read from a file like
that, carriage returns and newlines will be stripped out. If you
don't want the @ character to have a special interpretation use
--data-raw instead.

所以我试了...

C:\Users\testUser>"C:\Program Files\Git\mingw64\bin\curl.exe" "http://localhost:3000/todos/" -H "'Content-Type:' 'application/json'" --data-binary text=doing --data complete=false
{"text":"doing","complete":"false","id":145}
C:\Users\testUser>"C:\Program Files\Git\mingw64\bin\curl.exe" "http://localhost:3000/todos/" -H "'Content-Type:' 'application/json'" --data-binary text=ding
{"text":"ding","id":146}

但我无法弄清楚如何为 JSON 对象制作超过 1 个单词,而不是“做”,我需要“做某事”。似乎 MingW64 git curl 正在接受不同的格式......

最佳答案

修改curl命令后它就可以工作了,因为你需要为windows系统使用双引号。

修改命令后,您错误地在 application/json 周围添加了单引号。这就是为什么尽管有工作的 curl 命令服务器仍不确定您向他们发送了什么!

"Content-Type: 'application/json'"
^ ^ notice the unwanted singles

应该是这样

"Content-Type: application/json"

如果您没有为任何二进制文件(即 curl.exe、mysql.exe、php.exe 等)提供路径,那么系统会在 PATH 变量中提供的可用路径中查找它们,如果他们在那里找到了多个路径,然后它只会选择一个,我不知道是哪一个!

关于windows - Windows 中的 curl 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34171246/

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