0%

[linux] 2.curl命令行工具

curl 是常用的命令行工具(其实并非linux下的,windows cmd也有,只是我在linux下使用),用来请求 Web 服务器。它的名字就是 客户端(client)的 URL 工具的意思。

不带有任何参数时,curl 就是发出 GET 请求。

1
$ curl https://www.baidu.com

-d#

-d参数用于发送 POST 请求的数据体。

1
$ curl -d'login=2333&password=2333'-X POST https://google.com/login

-X#

-X参数 指定 HTTP 请求的方法。

1
$ curl -X POST https://google.com

-H#

-H参数 添加 HTTP 请求的标头

1
$ curl -H 'Accept-Language: en-US' https://google.com
上面命令添加 HTTP 标头Accept-Language: en-US。

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

上面命令 添加两个 HTTP 标头

1
$ curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login
上面命令 添加 HTTP 请求的标头是Content-Type: application/json,然后用-d参数发送 JSON 数据。

-v#

  • -v参数输出通信的整个过程,用于调试。
    1
    $ curl -v https://www.baidu.com
  • -trace参数也可以用于调试,还会输出原始的二进制数据。
1
$ curl --trace - https://www.baidu.com

-o#

-o参数将服务器的回应 保存成文件,等同于wget命令。

1
$ curl -o baidu.html https://www.baidu.com

上面命令将www.baidu.com保存成baidu.html。

报错curl: (35) Unknown SSL protocol error in connection to localhost:27183#

1
2
3
4
5
6
7
8
9
10
11
$ curl -v https://localhost:27183/abc
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 27183 (#0)
* successfully set certificate verify locations:
* CAfile: none
CApath: /etc/ssl/certs
* SSLv3, TLS handshake, Client hello (1):
* Unknown SSL protocol error in connection to localhost:27183
* Closing connection 0
curl: (35) Unknown SSL protocol error in connection to localhost:27183

Possible reason:没有证书,本地用 http不能用https