curl用法总结

iBit程序猿 2017年12月26日 1,065次浏览

1. 查看网页源码

$ curl [URL] //eg: curl ibit.tech

如果需要将网页保存下来,可以通过-o参数,这个相当于使用wget命令。

$ curl -o [文件名] [URL] //eg: curl -o ibit-tech.html ibit.tech

2. 自动跳转

使用-L自动跳转到新的网址

$ curl -L [URL]  //eg: curl -L http://ibit.tech,会自动跳转到 https://ibit.tech

3. 显示头信息

-i 参数显示http response,头部信息

$ curl -i [URL]

eg:

$ curl -i ibit.tech
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 26 Dec 2017 08:53:05 GMT
Content-Type: text/html
Content-Length: 3693
Last-Modified: Tue, 26 Dec 2017 07:11:06 GMT
Connection: keep-alive
ETag: "5a41f60a-e6d"
Accept-Ranges: bytes

//省略html源码

-I参数则是只显示http response的头信息。

4. 显示通信过程

-v 参数显示一次http通信的整个过程,包括端口连接和http request头信息

$ curl -v [URL]

eg:

$ curl -v ibit.tech
* Rebuilt URL to: ibit.tech/
*   Trying 39.106.33.200...
* TCP_NODELAY set
* Connected to ibit.tech (39.106.33.200) port 80 (#0)
> GET / HTTP/1.1
> Host: ibit.tech
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx/1.12.2
< Date: Tue, 26 Dec 2017 08:58:52 GMT
< Content-Type: text/html
< Content-Length: 3693
< Last-Modified: Tue, 26 Dec 2017 07:11:06 GMT
< Connection: keep-alive
< ETag: "5a41f60a-e6d"
< Accept-Ranges: bytes
<

//省略html源代码

更加详情的内容,打印到文件

$ curl --trace [文件名] [URL]  //eg: curl --trace ibittech.txt ibit.tech

或者

$ curl --trace-ascii [文件名] [URL]  //eg: curl --trace-ascii ibittech.txt ibit.tech

ibittech.txt 部分内容

== Info:   Trying 39.106.33.200...
== Info: TCP_NODELAY set
== Info: Connected to ibit.tech (39.106.33.200) port 443 (#0)
== Info: ALPN, offering h2
== Info: ALPN, offering http/1.1
== Info: successfully set certificate verify locations:
== Info:   CAfile: /etc/ssl/cert.pem
  CApath: none
== Info: TLSv1.2 (OUT), TLS handshake, Client hello (1):
=> Send SSL data, 223 bytes (0xdf)
0000: .......az0B1...&rB...4...A|.............\.0.,.(.$.......k.9.....
0040: ............=.5...../.+.'.#.......g.3...E...<./...A.............
0080: ........V.........ibit.tech.....................................
00c0: ....................h2.http/1.1
== Info: TLSv1.2 (IN), TLS handshake, Server hello (2):

5. 发送表单信息

通过GET方法

$ curl [URL][?p1=v1&p2=v2]  //eg: curl ibit.tech?timestamp=12367

通过POST方法, --data简化为-d

$ curl -X POST -d "p1=v1&p2=v2..." [URL]  //eg: curl -X POST -d 'timestamp=12345' ibit.tech

等价于:

$ curl -d "p1=v1&p2=v2..." [URL]  //`Content-Type: application/x-www-form-urlencoded`

使用POST方法,默认加上头信息:"Content-Type: application/x-www-form-urlencoded"

如果数据没有编码,使用--data-urlencode

$ curl -X POST --data-urlencode "p1=v1&p2=v2..." [URL]

使用POST发送json

$ curl -H 'Content-Type:application/json' -d '[JSON string]' [URL]

6. HTTP动词

curl 默认的HTTP动词是GET,使用-X参数可以支持其他动词。

$ curl -X POST [URL]    //eg: curl -X POST ibit.tech
$ curl -X DELETE [URL]  //eg: curl -X DELETE ibit.tech

7. 文件上传

假定文件上传表单为:

<form method="POST" enctype='multipart/form-data' action="#">
   <input type=file name=file>
   <input type=submit name= submit value="OK">
</form>

curl上传文件,使用--form或者-F,文件名前面记得加"@"

$ curl --form file=@[文件名] --form submit =OK [URL]

文件上传会增加头信息 "Content-Type: multipart/form-data;"

8. Referer字段

使用--referer或者-e参数

$ curl --referer [Referer URL] [URL]

9. User Agent字段

使用--user-agent 或者 -A 参数

$ curl --user-agent "[User Agent]" [URL]

使用--cookie 或者 -c 参数

$ curl --cookie "cn=cv" [URL] //eg: curl -c 'sid=xxx' ibit.tech

-c cookie-file可以保存服务器返回的cookie到文件(服务器返回Set-Cookie头部信息),-b cookie-file可以使用这个文件作为cookie信息,进行后续的请求。

$ curl -c [Cookie 文件] [URL]
$ curl -b [Cookie 文件] [URL]

11. 增加头部信息

使用--header或者-H参数

$ curl -H "hn: hv" [URL] //eg: curl -X POST -H 'Content-Type: application/json' -d '' ibit.tech

12. HTTP认证

使用--user 或者 -u参数

$ curl -u name:password [URL]

参考资料

阮一峰的 curl网站开发指南