草庐IT

windows - Windows 中的 curl 命令

coder 2024-06-21 原文

我正在按照以下链接学习 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/

有关windows - Windows 中的 curl 命令的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby - 其他文件中的 Rake 任务 - 2

    我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时

  3. ruby-on-rails - Ruby net/ldap 模块中的内存泄漏 - 2

    作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代

  4. ruby - 在 Ruby 程序执行时阻止 Windows 7 PC 进入休眠状态 - 2

    我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0

  5. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  6. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  7. ruby - 在 Ruby 中编写命令行实用程序 - 2

    我想用ruby​​编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序

  8. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  9. ruby-on-rails - Rails 应用程序中的 Rails : How are you using application_controller. rb 是新手吗? - 2

    刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr

  10. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

随机推荐