GET

  1. $ curl douban.com
  2. <html>
  3. <head><title>301 Moved Permanently</title></head>
  4. <body>
  5. <center><h1>301 Moved Permanently</h1></center>
  6. <hr><center>nginx</center>
  7. </body>
  8. </html>

可以看见,上述返回 301 永久重定向,可以使用 curl 中的 -i/--include 选项,返回完整的 HTTP Response 信息:

  1. $ curl -i douban.com
  2. HTTP/1.1 301 Moved Permanently
  3. Date: Thu, 19 Nov 2020 02:31:39 GMT
  4. Content-Type: text/html
  5. Content-Length: 162
  6. Connection: keep-alive
  7. Keep-Alive: timeout=30
  8. Location: http://www.douban.com/
  9. Server: dae
  10. Strict-Transport-Security: max-age=15552000;
  11. <html>
  12. <head><title>301 Moved Permanently</title></head>
  13. <body>
  14. <center><h1>301 Moved Permanently</h1></center>
  15. <hr><center>nginx</center>
  16. </body>
  17. </html>

如果你只想返回头信息,可以使用 -I/--head

  1. $ curl -I douban.com
  2. HTTP/1.1 301 Moved Permanently
  3. Date: Thu, 19 Nov 2020 02:34:17 GMT
  4. Content-Type: text/html
  5. Content-Length: 162
  6. Connection: keep-alive
  7. Keep-Alive: timeout=30
  8. Location: http://www.douban.com/
  9. Server: dae
  10. Strict-Transport-Security: max-age=15552000;

从头信息中我们可以看出,他被重定向到了 http://www.douban.com/ 上,我们可以使用 -L/--location 自动重定向

  1. $ curl -L douban.com
  2. ... # 内容较多,这里省略

下载文件

由于上面输出内容较多,我们使用 -o/--output 来将输出的内容下载成文件:

  1. $ curl -o douban.html douban.com
  2. % Total % Received % Xferd Average Speed Time Time Time Current
  3. Dload Upload Total Spent Left Speed
  4. 100 162 100 162 0 0 1408 0 --:--:-- --:--:-- --:--:-- 1396

也可以指定 -O 来下载文件,他会自动以末尾的 URI 文件名命名,所以如果 URI 末尾没有文件名,他将会失败:

  1. $ curl -O https://wangdoc.com/bash/intro.html
  2. % Total % Received % Xferd Average Speed Time Time Time Current
  3. Dload Upload Total Spent Left Speed
  4. 100 21700 0 21700 0 0 7209 0 --:--:-- 0:00:03 --:--:-- 7211

显示通信过程

如果你想更详细的查看网络的通信过程,可以使用 -v/--verbose ,也称啰嗦模式

  1. $ curl -v douban.com
  2. * Trying 154.8.131.171:80...
  3. * Connected to douban.com (154.8.131.171) port 80 (#0)
  4. > GET / HTTP/1.1
  5. > Host: douban.com
  6. > User-Agent: curl/7.73.0
  7. > Accept: */*
  8. >
  9. * Mark bundle as not supporting multiuse
  10. < HTTP/1.1 301 Moved Permanently
  11. < Date: Thu, 19 Nov 2020 03:04:10 GMT
  12. < Content-Type: text/html
  13. < Content-Length: 162
  14. < Connection: keep-alive
  15. < Keep-Alive: timeout=30
  16. < Location: http://www.douban.com/
  17. < Server: dae
  18. < Strict-Transport-Security: max-age=15552000;
  19. <
  20. <html>
  21. <head><title>301 Moved Permanently</title></head>
  22. <body>
  23. <center><h1>301 Moved Permanently</h1></center>
  24. <hr><center>nginx</center>
  25. </body>
  26. </html>
  27. * Connection #0 to host douban.com left intact

也可以使用 --trace--trace-ascii 查看更详细的信息:

  1. $ curl --trace output.txt douban.com
  2. $ curl --trace-ascii output.txt douban.com # 以 ascii 码的形式输出

POST 方法

可以直接使用 -d 来使用使用 POST 方法来传递数据:
比如,http://www.formpost.com/getthis/ 网站上有这样一个表单:

  1. <form action="post.cgi" method="post">
  2. <input name=user size=10>
  3. <input name=pass type=password size=10>
  4. <input name=id type=hidden value="blablabla">
  5. <input name=ding value="submit">
  6. </form>

就可以使用下面这段命令来请求:

  1. $ curl -d "user=foobar&pass=12345&id=blablabla&ding=submit"
  2. http://www.formpost.com/getthis/post.cgi

-d 选项其实会自动加上 HTTP 请求头:Content-Type : application/x-www-form-urlencoded,并将其自动转化为 POST 方法,因此可以省略 -X POST
可以使用非简写的命令:

  1. $ curl --header "Content-Type: application/x-www-form-urlencoded" \
  2. --request POST \
  3. --data "user=foobar&pass=12345&id=blablabla&ding=submit" \
  4. http://www.formpost.com/getthis/post.cgi

添加 HTTP 请求标头

从上面可以看出,使用 -H/--header 可以添加 HTTP 请求标头:

  1. $ curl -H 'Accept-Language: en-US' https://google.com

添加数据

从上面可以看出,使用 --data 可以传递数据:

  1. $ curl --data 'comment=hello%20world' https://google.com/login

也可以使用 --data-urlencode 自动进行 URL 编码:

  1. $ curl --data-urlencode 'comment=hello world' https://google.com/login

注意这里的 hello world 中间的空格不用自己编码

样例

  1. $ curl -v --noproxy "*" www.baidu.com

其中:

  • -v 可以更详细的查看网络通信过程。
  • --noproxy "*" 表示在请求过程中禁用代理。