curl详细用法
簡(jiǎn)介
curl是一個(gè)和服務(wù)器交互信息(發(fā)送和獲取信息)的命令行工具,支持DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET和TFTP等協(xié)議。curl支持代理、用戶(hù)認(rèn)證、FTP上傳、HTTP POST請(qǐng)求、SSL連接、cookies、文件傳輸、Metalink等功能。
URL
curl支持如下幾種方式的URL:
可以指定多個(gè)url,或者在花括號(hào)中指定url的多個(gè)部分。
http://site.{one,two,three}.com可以用中括號(hào)指定數(shù)字或字母序列。
ftp://ftp.numericals.com/file[1-100].txt ftp://ftp.numericals.com/file[001-100].txt (with leading zeros) ftp://ftp.letters.com/file[a-z].txt可以指定多個(gè)序列。
http://any.org/archive[1996-1999]/vol[1-4]/part{a,b,c}.html可以在命令行指定任意數(shù)量的url,curl會(huì)按指定順序獲取url的內(nèi)容。
可以在一個(gè)范圍內(nèi)指定跳躍的步數(shù)。
http://www.numericals.com/file[1-100:10].txt http://www.letters.com/file[a-z:2].txt如果沒(méi)有指定協(xié)議前綴,curl會(huì)嘗試猜測(cè)協(xié)議。它默認(rèn)會(huì)選擇http協(xié)議,但是當(dāng)遇見(jiàn)常用的host名字時(shí),會(huì)選擇嘗試其他協(xié)議。例如ftp.xxx.com,curl會(huì)嘗試ftp協(xié)議。
查看http響應(yīng)頭
curl -i http://www.baidu.com查看交互過(guò)程
curl -v http://www.baidu.comGET請(qǐng)求
當(dāng)發(fā)起http請(qǐng)求時(shí),curl會(huì)默認(rèn)發(fā)起GET請(qǐng)求,也可以"-X GET"方式指定。
curl -X GET http://www.baidu.comPOST請(qǐng)求
當(dāng)使用POST請(qǐng)求方式,需要通過(guò)指定“-d”,向服務(wù)器傳遞數(shù)據(jù)。
curl -X POST http://www.example.com/postsDELETE請(qǐng)求
DELETE請(qǐng)求用于刪除服務(wù)器端的數(shù)據(jù)。
curl -X DELETE http://www.example.com/posts/1PUT請(qǐng)求
PUT請(qǐng)求用于修改服務(wù)器端的數(shù)據(jù)
curl -X PUT http://www.example.com/posts/1HTTP認(rèn)證
常用的HTTP認(rèn)證方式有:Basic認(rèn)證、Digest認(rèn)證、OAuth2認(rèn)證。
Basic認(rèn)證
curl --basic -u user:password http://www.example.com/posts/1Digest認(rèn)證
curl --digest -u user:password http://www.example.com/posts/1OAuth2認(rèn)證
curl -u clientId:clientSecret -X POST -d "username=test&password=test&grant_type=password&scope=read" http://www.example.com/oauth/token curl -H "Authorization: Bearer [bearer]" http://www.example.com/posts/1文件上傳
假定文件上傳的表單如下所示:
<form method="POST" enctype='multipart/form-data' action="upload.cgi"><input type=file name=upload><input type=submit name=press value="OK"> </form>可使用curl按如下方式上傳文件:
curl --form upload=@localfilename --form press=OK http://www.example.comUser Agent字段
這個(gè)字段用來(lái)表示客戶(hù)端的設(shè)備信息。服務(wù)器有時(shí)會(huì)根據(jù)這個(gè)字段,針對(duì)不同的設(shè)備,返回不同格式的網(wǎng)頁(yè),比如移動(dòng)端和PC端。
curl --user-agent "[user agent]" http://www.example.comcookie
curl可以發(fā)送cookie
curl --cookie "name1=value1" http://www.example.com下載網(wǎng)頁(yè)
curl -o file.html http://www.example.com-O選項(xiàng)可以按照服務(wù)器的文件名保存文件
curl -O http://www.example.com/1.jpg代理服務(wù)器
curl -x 代理服務(wù)器地址:端口 http://www.example.com保存cookie信息
curl -D cookiefile01.txt http://www.example.com使用保存cookie信息的文件
curl -D cookiefile02.txt -b cookiefile01.txt http://www.example.com輸出詳細(xì)的交互信息
curl http://www.example.com --trace-ascii /dev/stdout總結(jié)
- 上一篇: go get安装第三方包的前提条件和步骤
- 下一篇: Docker使用Link在容器之间建立连