curl命令

📅 2026/6/26 15:15:23
curl命令
有读者应该用过图形化的 http 请求模拟工具PostMan与 PostMan 一样curl命令是 Linux 上可以模拟发送 http 请求的一个非常常用的命令可以使用如下命令安装yum install curlcurl最基础的用法是curl 页面url例如curl http://www.baidu.com其默认行为是把目标页面内容输出到 shell 窗口[rootlocalhost ~]# curl http://www.baidu.com !DOCTYPE html !--STATUS OK--html headmeta http-equivcontent-type contenttext/html;charsetutf-8meta http-equivX-UA-Compatible contentIEEdgemeta contentalways namereferrerlink relstylesheet typetext/css hrefhttp://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.csstitle百度一下你就知道/title/head body link#0000cc div idwrapper div idhead div classhead_wrapper div classs_form div classs_form_wrapper div idlg img hidefocustrue src//www.baidu.com/img/bd_logo1.png width270 height129 /div form idform namef action//www.baidu.com/s classfm input typehidden namebdorz_come value1 input typehidden nameie valueutf-8 input typehidden namef value8 input typehidden namersv_bp value1 input typehidden namersv_idx value1 input typehidden nametn valuebaiduspan classbg s_ipt_wrinput idkw namewd classs_ipt value maxlength255 autocompleteoff autofocus/spanspan classbg s_btn_wrinput typesubmit idsu value百度一下 classbg s_btn/span /form /div /div div idu1 a hrefhttp://news.baidu.com nametj_trnews classmnav新闻/a a hrefhttp://www.hao123.com nametj_trhao123 classmnavhao123/a a hrefhttp://map.baidu.com nametj_trmap classmnav地图/a a hrefhttp://v.baidu.com nametj_trvideo classmnav视频/a a hrefhttp://tieba.baidu.com nametj_trtieba classmnav贴吧/a noscript a hrefhttp://www.baidu.com/bdorz/login.gif?loginamp;tplmnamp;uhttp%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 nametj_login classlb登录/a /noscript scriptdocument.write(a hrefhttp://www.baidu.com/bdorz/login.gif?logintplmnu encodeURIComponent(window.location.href (window.location.search ? ? : ) bdorz_come1) nametj_login classlb登录/a);/script a href//www.baidu.com/more/ nametj_briicon classbri styledisplay: block;更多产品/a /div /div /div div idftCon div idftConw p idlh a hrefhttp://home.baidu.com关于百度/a a hrefhttp://ir.baidu.comAbout Baidu/a /p p idcpcopy;2017nbsp;Baidunbsp;a hrefhttp://www.baidu.com/duty/使用百度前必读/anbsp; a hrefhttp://jianyi.baidu.com/ classcp-feedback意见反馈/anbsp;京ICP证030173号nbsp; img src//www.baidu.com/img/gs.gif /p /div /div /div /body /html你也可以把页面保存到本地等价于-o选项示例curl http://www.baidu.com index.html ## 等价于 curl -o index.html http://www.baidu.comhttp 常用的请求方式是 GET 和 POST 两种方式我们可以使用-X选项来显式指定请求是GET还是POST方式不指定使用默认 GET 方式curl -X GET http://www.baidu.com/index.php?s9如果使用GET方式curl提供了另外一个专门的选项-G或--get来设置。如果是POST方法除了需要使用-X选项或--request指定请求方法还需要使用-d或--data指定 POST 的数据内容curl -X POST -d somepostdata https://www.somesite.com/api/v1/chat我们有时候需要在发送 http 请求时设置增加一些头部信息可以使用-H或--header指定如果有多个选项可以使用多次-H来逐一设置。例如curl -X POST -H Content-Type: application/x-www-form-urlencoded -H Accept: application/json -H X-Requested-With: XMLHttpRequest -d somepostdata https://www.somesite.com/api/v1/chat当然对于一些常用的 http 头字段curl提供了单独的选项例如user-agent字段可以使用-A选项或--user-agent设置referer字段可以使用-e选项或--referer设置等等。如果你希望 http 的应答结果中包含 http 头部信息可以使用-i选项或--include演示如下[rootlocalhost ~]# curl -i http://www.baidu.com/ 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: Sat, 09 Mar 2019 06:47:15 GMT Etag: 588604c4-94d Last-Modified: Mon, 23 Jan 2017 13:27:32 GMT Pragma: no-cache Server: bfe/1.0.8.18 Set-Cookie: BDORZ27315; max-age86400; domain.baidu.com; path/ !DOCTYPE html !--STATUS OK--html headmeta http-equivcontent-type contenttext/html;charsetutf-8meta http-equivX-UA-Compatible contentIEEdgemeta contentalways namereferrerlink relstylesheet typetext/css hrefhttp://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.csstitle百度一下你就知道/title/head body link#0000cc div idwrapper div idhead div classhead_wrapper div classs_form div classs_form_wrapper div idlg img hidefocustrue src//www.baidu.com/img/bd_logo1.png width270 height129 /div form idform namef action//www.baidu.com/s classfm input typehidden namebdorz_come value1 input typehidden nameie valueutf-8 input typehidden namef value8 input typehidden namersv_bp value1 input typehidden namersv_idx value1 input typehidden nametn valuebaiduspan classbg s_ipt_wrinput idkw namewd classs_ipt value maxlength255 autocompleteoff autofocus/spanspan classbg s_btn_wrinput typesubmit idsu value百度一下 classbg s_btn/span /form /div /div div idu1 a hrefhttp://news.baidu.com nametj_trnews classmnav新闻/a a hrefhttp://www.hao123.com nametj_trhao123 classmnavhao123/a a hrefhttp://map.baidu.com nametj_trmap classmnav地图/a a hrefhttp://v.baidu.com nametj_trvideo classmnav视频/a a hrefhttp://tieba.baidu.com nametj_trtieba classmnav贴吧/a noscript a hrefhttp://www.baidu.com/bdorz/login.gif?loginamp;tplmnamp;uhttp%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 nametj_login classlb登录/a /noscript scriptdocument.write(a hrefhttp://www.baidu.com/bdorz/login.gif?logintplmnu encodeURIComponent(window.location.href (window.location.search ? ? : ) bdorz_come1) nametj_login classlb登录/a);/script a href//www.baidu.com/more/ nametj_briicon classbri styledisplay: block;更多产品/a /div /div /div div idftCon div idftConw p idlh a hrefhttp://home.baidu.com关于百度/a a hrefhttp://ir.baidu.comAbout Baidu/a /p p idcpcopy;2017nbsp;Baidunbsp;a hrefhttp://www.baidu.com/duty/使用百度前必读/anbsp; a hrefhttp://jianyi.baidu.com/ classcp-feedback意见反馈/anbsp;京ICP证030173号nbsp; img src//www.baidu.com/img/gs.gif /p /div /div /div /body /html在有些情况下如果只想显示 http 应答的头部信息不是 html 文档头部可以使用-I选项或- -head演示如下[rootlocalhost ~]# curl -I http://www.baidu.com/ HTTP/1.1 200 OK Accept-Ranges: bytes Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform Connection: Keep-Alive Content-Length: 277 Content-Type: text/html Date: Sat, 09 Mar 2019 06:42:16 GMT Etag: 575e1f8a-115 Last-Modified: Mon, 13 Jun 2016 02:50:50 GMT Pragma: no-cache Server: bfe/1.0.8.18注意上述输出结果中13行的空行是刻意保留的http 协议的头部每一行以 \r\n 结束整个头部再以一个 额外的 \r\n 结束所以正好末尾是有个空行。如果你需要使用代理去发送 http 请求可以使用-x选项或--proxy使用形式如下curl -x [protocol://][user:password]proxyhost[:port]上面介绍的curl选项有点多读者不必刻意记住每个选项只需要记住常用的即可其他的实际需要时去 man 手册查看帮助即可。