【发布时间】:2016-03-14 06:42:42
【问题描述】:
我正在按照下面的链接学习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 下运行时,它显示 'curl' 在 cmd 提示符下无法识别。
如果我尝试使用 git-bash.exe、bash.exe 或 sh.exe(在 Git/bin 或 shell.w32-ix86 中)、Cygwin.bat(在 cygwin64 中)运行它,它将运行良好并显示结果在浏览器中。
[{"text":"Do something","id":0}]
但是如果尝试通过将它包含到我的路径“C:\Program Files\Git\mingw64\bin, which has curl.exe”来运行它,我将收到以下错误,但是“C:\Program Files\Git \usr\bin" 就可以了...
curl:(1) libcurl 中不支持或禁用协议“'http”
curl:(6)无法解析主机“应用程序”
curl:(6)无法解析主机文本'
curl:(6)无法解析主机“做某事”
curl: (3) [globbing] 第 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等2下可以正常运行?
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 == "应用程序/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 '<br> at Object.parse (native)<br> at parse (C:\Users\testUser\Documents\Framework\Javascript\featherstestNewService\node_modules\body-parser\lib\types\json.js:88:17)<br> at C:\Users\testUser\Documents\Framework\Javascript\featherstestNewService\node_modules\body-parser\lib\read.js:116:18<br> at invokeCallback (C:\Users\testUser\Documents\Framework\Javascript\featherstestNewService\node_modules\body-parser\node_modules\raw-body\index.js:262:16)<br> at done (C:\Users\testUser\Documents\Framework\Javascript\featherstestNewService\node_modules\body-parser\node_modules\raw-body\index.js:251:7)<br> at IncomingMessage.onEnd (C:\Users\testUser\Documents\Framework\Javascript\featherstestNewService\node_modules\body-parser\node_modules\raw-body\index.js:308:7)<br> at emitNone (events.js:67:13)<br> at IncomingMessage.emit (events.js:166:7)<br> at endReadableNT (_stream_readable.js:905:12)<br> 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 正在接受不同的格式...
【问题讨论】: