http请求过程如下:
# curl -v http://www.baidu.com
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* About to connect() to www.baidu.com port 80 (#0)
* Trying 220.181.112.244...
* Connected to www.baidu.com (220.181.112.244) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.baidu.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Connection: Keep-Alive
< Content-Length: 2381
< Content-Type: text/html
< Date: Wed, 16 Jan 2019 13:21:06 GMT
< Etag: "588604c8-94d"
< Last-Modified: Mon, 23 Jan 2017 13:27:36 GMT
< Pragma: no-cache
< Server: bfe/1.0.8.18
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
<
{ [data not shown]
100 2381 100 2381 0 0 105k 0 --:--:-- --:--:-- --:--:-- 110k
* Connection #0 to host www.baidu.com left intact
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=
http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head>
...
工作中经常需要模拟http请求(比如任意修改header和body),curl非常好用,可以通过
curl -h
常看详细的参数列表,下面列一些常用参数:
-v, --verbose Make the operation more talkative
显示详细的request和response信息,主要是header、状态码等
-u, --user USER[:PASSWORD] Server user and password
指定登陆账号和密码,-u user:password
-X, --request COMMAND Specify request command to use
指定Method,比如Get、Post、Put、Delete等,-X GET
-x, --proxy [PROTOCOL://]HOST[:PORT] Use proxy on given port
指定代理服务器和端口
-o, --output FILE Write output to <file> instead of stdout
输出内容到指定文件,下载时使用
-H, --header LINE Custom header to pass to server (H)
指定任何header,-H 'Cookie: anything'
-d, --data DATA HTTP POST data (H)
指定post的body,-d '{"name":"value"}'
还可以通过-d指定一个文件的内容,比如 -d@/path/to/file
-F, --form <name=content> Specify multipart MIME data
支持上传文件, 比如 -F "filename=@/test/test.tar.gz"