我正在学习羽毛js,如下面链接
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}]
但如果尝试通过包含到我的PATH“C:\ Program Files \ Git \ mingw64 \ bin,它有curl.exe”来运行它,我将得到以下错误,但“C:\ Program Files \ Git \ usr \ bin“会做得很好......
curl:(1)libcurl中不支持或禁用协议“'http” 卷曲:(6)无法解决主持人'申请' 卷曲:(6)无法解析主题文本 卷曲:(6)无法解决主持人'做某事' 卷曲:(3)第1列中[globbing]无与伦比的紧密支撑/支架
我设法通过以下命令
删除了错误curl "http://localhost:3000/todos/" -H "Content-Type: 'application/json'" --data-binary "{ 'text': 'Do something' }"
但是结果输出会丢失Json对象“text”......
[{"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?
更新
不正确的Raw Cap输出 http.content_type ==“'application / json'”
OK Raw Cap输出 http.content_type ==“application / json”
UPDATE2:
在第二个命令中删除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的/ 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
来自手动...卷曲 - 手动
-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接受不同的格式......
答案 0 :(得分:2)
修改curl
命令后,它适用于您,因为您需要为Windows系统使用双引号。
修改命令后,您错误地在application/json
周围添加了单引号。这就是为什么尽管有工作curl命令服务器,但不确定你究竟发送给他们的是什么!
"Content-Type: 'application/json'"
^ ^ notice the unwanted singles
所以它应该是
"Content-Type: application/json"
如果您没有为任何二进制文件(即curl.exe,mysql.exe,php.exe等)提供路径,那么系统会在PATH
变量中提供的可用路径中查找它们,如果它们找到多个那里的路径只会选择一个,我不知道哪一个!