对于http调试,curl是一个很好用的工具,本篇文章主要记录curl平常的使用方法。html
访问urljson
$ curl http://www.baidu.com % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 251 100 251 0 0 7843 0 --:--:-- --:--:-- --:--:-- 7843<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=gb2312"><TITLE>302 Moved</TITLE></HEAD><BODY><H1>302 Moved</H1>The document has moved<A HREF="http://www.baidu.com/?tn=28035039_3_pg&ld=www.baidu.com/">click here</A></BODY></HTML>
最基本的使用方法,访问一个url,输出返回结果。服务器
get参数
curl默认使用get方式访问url,因此能够直接在url后添加参数cookie
curl http://localhost:8080/hello?name=bin\&content=hello
注意:url中用&拼接参数时,须要使用\&
进行转码app
修改http header参数curl
curl -H "Accept-Language: zh-cn" -H "Accept: application/html" http://www.baidu.com
上面栗子中经过-H修改http header参数,-H能够使用屡次,以修改多个参数。工具
POST JSON
开发中咱们常须要使用POST JSON的方法来访问咱们的服务器:post
curl -H 'Content-Type: application/json; charset=UTF-8' \ -X POST -d '{"name":"binecy","content":"hello"}' \ http://localhost:8080/hello
-X POST
指定使用post提交-d
添加post json内容ui
post表单参数
除了post json,咱们还经常须要post 表单的键值对参数url
curl -X POST -d 'nam=bin&content=hello' \ http://localhost:8080/hello
cookie信息
开发中经常在cookie中携带用户的登录信息,因此访问url时也要携带cookie信息:
curl -H 'Content-Type: application/json; charset=UTF-8' --cookie "admin_token=00000763aZQ8GP5U" \ -X POST -d '{"name":"binecy","content":"hello"}' \ http://localhost:8080/hello
经过--cookie 添加cookie信息,
--cookie也能够写为-b
显示http头部信息
添加-v参数能够让curl显示http的请求和响应的header信息,方便咱们调试
$ curl http://www.baidu.com -v * Rebuilt URL to: http://www.baidu.com/ * timeout on name lookup is not supported * Trying 14.215.177.39... * TCP_NODELAY set % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Connected to www.baidu.com (14.215.177.39) port 80 (#0) > GET / HTTP/1.1 > Host: www.baidu.com > User-Agent: curl/7.54.0 > Accept: */* > < HTTP/1.1 302 Found < Location: http://www.baidu.com/?tn=28035039_3_pg&ld=www.baidu.com/ < Content-Type: text/html;charset=gb2312 < Pragma: no-cache < Cache-Control: no-cache < Connection: close < Server: wys < Content-Length: 251 < { [251 bytes data] 100 251 100 251 0 0 15687 0 --:--:-- --:--:-- --:--:-- 15687<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=gb2312"><TITLE>302 Moved</TITLE></HEAD><BODY><H1>302 Moved</H1>The document has moved<A HREF="http://www.baidu.com/?tn=28035039_3_pg&ld=www.baidu.com/">click here</A></BODY></HTML> * Closing connection 0
好了,本篇文章比较简单,但对于curl的平常使用,也基本足够了。