Curl

基本用法

1
2
3
4
curl http://www.google.com
curl --proxy http://proxy.com:8888 http://ww.google.com #使用代理
curl http://user:password@example.org/  #http验证
curl -u user:password http://example.org/ #http验证

获取响应头

1
2
curl -i http://example.com  #在返回结果中包含响应头 
curl -IL http://example.com  #仅返回响应头

REST请求

GET

1
curl http://www.google.com

POST

1
2
3
curl --data "birthyear=1905&press=%20OK%20"  http://www.example.com/when.cgi
#or
curl --data-urlencode "name=I am Daniel" http://www.example.com

PUT

1
curl --upload-file uploadfile http://www.example.com/receive.cgi #upload a file

DELETE

实际上,可以用curl -X http_method指定包括GET,POST,PUT内的任意方法

1
curl -X DELETE http://example.com

使用-H或–header指定请求头部

1
curl -H "Content-Type: application/json" http:/example.com

-b,--cookie选项告诉curl使用已有的cookie,可以是键值对也可以是文件

1
2
curl -b "name=value" http://host
curl -b cookie.txt http://host.com

-c,--cookie-jar选项告诉curl将新的cookie保存在文件中

1
curl -c new.txt http://example.com

HTTPS

1
2
curl https://example.com
curl --cert mycert.pem https://example.com

Comments