curl(CommandLine Uniform Resource Locator或CommandLine URL),curl命令是在命令行方式下工作,利用URL的语法进行数据的传输或者文件的传输。是一个利用URL语法在命令行下工作的文件传输工具。curl设计为:在无用户交互下完成工作。
curl支持的协议包括:DICT、FILE、FTP、FTPS、GOPHER、HTTP、HTTPS、IMAP、IMAPS、LDAP、LDAPS、POP3、POP3S、RTMP、RTSP、SCP、SFTP、SMTP、SMTPS、TELNET、TFTP。
curl [options…] <url>
参数类型 | 参数 | 描述 |
---|---|---|
请求 | -H "name: value" --header "name: value" | (HTTP)添加一个http header(http请求头) |
-H "name:" --header "name:" | (HTTP)移除一个http header(http请求头) | |
-A "string" --user-agent "string" | (HTTP)设置HTTP请求头User-Agent参数 服务器通过User-Agent可以判断客户端使用的浏览器名称和操作系统类型,伪造此参数能导致服务器做出错误判断 | |
-e --referer | (HTTP)设置访问时的来源页面,告诉http服务从哪个页面进入到此页面 例如:-e "aiezu.com"相当于“-H "Referer: www.qq.com"”; | |
-G --get | (HTTP)使用HTTP GET方式 | |
-d @file -d "string" --data "string" --data-ascii "string" --data-binary "string" --data-urlencode "string" | (HTTP)使用HTTP POST方式发送“key/value对”数据 相当于(method="POST",enctype="application/x-www-form-urlencoded") -d,--data:HTTP方式POST数据 --data-ascii:HTTP方式POST ascii数据 --data-binary:HTTP方式POST二进制数据 --data-urlencode:HTTP方式POST数据(进行urlencode) 如果数据以“@”开头,后紧跟一个文件,将post文件内的内容; | |
-F name=@file -F name=<file -F name=content --form name=content --form-string <key=value> | (HTTP)使用HTTP POST方式发送类似“表单字段”的多类型数据 相当于同时设置浏览器表单属性(method="POST",enctype="multipart/form-data"),可以使用此参数上传二进制文件。 如果字段内容以“@”开头,剩下的部分应该是文件名,curl将会上传此文件,如: curl -F "pic=@pic.jpg" http://aiezu.com; curl -F "page=@a.html;type=text/html" http://aiezu.com curl -F "page=@/tmp/a;filename=a.txt" http://aiezu.com 如果字段内容以“<”开头,剩下的部分应该是文件名,curl将从文件中获取作为此字段的值,如:curl -F "text=<text.txt" http://aiezu.com; --form-string类似于“--form”,但是“@”、“<”无特殊含义; | |
-X <command> --request <command> | (HTTP)指定与服务器通信使用的请求方法 如:GET、PUT、POST、DELETE等 | |
cookie | -b name=data --cookie name=data | (HTTP)发送cookie数据到HTTP服务器 数据格式为:"NAME1=VALUE1; NAME2=VALUE2" 如果行中没有“=”,将把参数值当作cookie文件名 这个cookie数据可以是由服务器的http响应头“Set-Cookie:”行发送过来的; |
-c filename --cookie-jar file name | (HTTP)完成操作后将服务器返回的cookies保存到指定的文件 指定参数值为“-”将定向到标准输出“如控制台” | |
-j --junk-session-cookies | (HTTP)告诉curl放弃所有的"session cookies".相当于重启浏览器 | |
响应 | -I --head | (HTTP)只输出HTTP-header,不获取内容(HTTP/FTP/FILE) 1. 用于HTTP服务时,获取页面的http头,如:curl -I http://aiezu.com 2. 用于FTP/FILE时,将会获取文件大小、最后修改时间,如:curl -I file://test.txt |
-i --include | (HTTP)输出HTTP头和返回内容 | |
-D <file> --dump-header <file> | (HTTP)转储http响应头到指定文件 |
curl 'http://127.0.0.1:8888/test'
curl -G 'http://127.0.0.1:8888/test'
curl --get 'http://127.0.0.1:8888/test'
curl -X GET 'http://127.0.0.1:8888/test'
curl --request GET 'http://127.0.0.1:8888/test'
当有参数时,请进行URL编码,否则会可能会出错
curl -X GET 'http://127.0.0.1:8888/test2?date=2022-02-22 11:22:33&bo=1'
报错:HTTP Status 505 – HTTP Version Not Supported(因为参数带空格)
curl -X GET 'http://127.0.0.1:8888/test2?date=2022-02-22%2011:22:33&bo=1'
curl -X POST 'http://127.0.0.1:8888/test2?date=2022-02-22%2011:22:33&bo=11'
curl -X POST 'http://127.0.0.1:8888/test2' -d 'date=2022-02-22 11:22:33&bo=11'
curl -X POST 'http://127.0.0.1:8888/test2' --data 'date=2022-02-22 11:22:33&bo=11'
curl -X POST 'http://127.0.0.1:8888/test2' --data-urlencode 'date=2022-02-22 11:22:33' --data-urlencode 'bo=11'
curl -X POST -H 'Content-Type: application/x-www-form-urlencoded' 'http://127.0.0.1:8888/test2' --data-urlencode 'date=2022-02-22 11:22:33' --data-urlencode 'bo=11'
POST请求默认Content-Type: application/x-www-form-urlencoded
可以多次使用 -d、–data选项来指定多个传输参数,也可以使用一次 -d、–data选项来指定多个参数。当 Content-Type 为 application/x-www-form-urlencoded 时,-X POST 不可缺省。
curl -X POST 'http://127.0.0.1:8888/test8' -H 'Content-Type: application/json' -d '{"code": 123456789,"name": 1646278385537}'
curl 'http://127.0.0.1:8888/test8' -H 'Content-Type: application/json' -d '{"code": 123456789,"name": 1646278385537}'
Content-Type:application/json 时,默认以 POST 方式提交,此时 -X POST 可以缺省。
参考文章: