常用命令
curl www.baidu.com get百度
curl -X POST www.baidu.com
curl -X POST --data "shit=happens" www.baidu.com
curl -X POST --data-urlencode "shit=happens" www.baidu.com 表单编码,post提交
curl -i www.baidu.com 显示header信息
curl -o test.txt www.baidu.com 保存
curl -v test.txt www.baidu.com 显示通信过程
curl --user-agent "Mozilla/5.0" www.baidu.com
curl --cookie "name=xxx" www.baidu.com
curl --header "Content-Type:applicaiton/json" www.baidu.com
curl --user name:password www.baidu.com
一次curl,多个URL
空格隔开就行;jianshu.com/p/fc0eb6c60816
GET命令的多个参数问题
会有&在URL中,不同操作系统的写法不同https://blog.csdn.net/sinat_27818621/article/details/104517435
linux:
curl localhost:8080/test?a=1\&b=2windows
curl -s "http://localhost:8080/get?name=zhangsan&age=12&sex=1"CookBook
Make a POST Request (TLDR: Use -X POST argument)
-
curl -X POST https://sth.com
-
Send a POST Request with Form Data
curl -d 'a=1&b=2' -X POST https://sth.comWhen using the
-dargument, curl also sets theContent-Typeheader toapplication/x-www-form-urlencoded.Note that the
key=valuedata should be URL-escaped. -
curl -d 'a=1&b=2' https://sht.comWhen
-dis used, curl implicitly sets the request's type to POST. -
curl -d 'a=1' -d 'b=2' https://sth.com
-
Send a POST Request with JSON Data
curl -d '{"a":2,"b":1}' -H 'Content-Type:application/json' https://sth.com
必须手动声明Content-Type头为application/json
-
Send a POST Request with XML Data
curl -d '<user>1</user>' -H 'Content-Type:text/xml' https://sth.com
注意Content-Type
-
Send a POST Request with Plain Text Data
curl -d 'hello world' -H 'Content-Type:text/plain' https://sht.com
注意Content-Type
-
So far, all recipes have been using the
-dargument to add POST data to requests. This argument assumes that your data is URL-encoded already. If it is not, then there can be some trouble. If your data is not URL-encoded, then replace-dwith--data-urlencode. It works exactly the same way as-d, except the data gets URL-encoded by curl before it's sent out on the wire.curl --data-urlencode 'a=1' https://sth.com-d是假设参数已经url编码好了的,--data-urlencode是对参数执行url编码
Add POST Data to a Request (TLDR: Use -d var=val argument)
同上一个
Construct a Query String (TLDR: Use -G argument)
This curl recipe shows you how to construct query strings for your GET requests. This is done via the
-Gcommand line argument in combination with the-dor--data-urlencodearguments. The-Gargument will append the data specified in-dand--data-urlencodearguments at the end of the request URL, joining all data pieces with the&character and separating them from the URL with the?character.
使用-G和-d或者--data-urlencode,把参数按照?&&的格式拼接到url的后面
-
curl -G -d 'a=1' -d 'b=2' https://sth.com
Be careful – if you forget the
-Gargument, then curl will make a POST request instead! -
curl -G --data-urlencode 'comment=this cookbook is awesome' https://catonmat.net
Add HTTP Headers (TLDR: Use -H 'Header: Value' argument)
curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://google.com
curl -H 'Puppies;' https://google.comChange the User Agent (TLDR: Use -A 'User Agent' argument)
浏览器啥的设置
Set Cookies (TLDR: Use -b name=value argument)
-
curl -b 'user_id=abc' https://sth.com在一个get请求中,添加了一个cookie,Cookie: user_id=abc
-
curl -b 'session=abcdef' -b 'loggedin=true' https://google.com
Add a Referrer (TLDR: Use -e URL argument)
信任来源设置
Follow a 3XX Redirect (TLDR: Use -L argument)
重定向
Use the Basic HTTP Authentication (TLDR: Use -u user:pass argument)
基础的HTTP登录,可以传递用户名和密码的
curl -u 'bob:12345' https://google.com/loginPrint the Response Headers (TLDR: Use -i argument)
-
Print the Response Headers and Body (together)
curl -i https://catonmat.net
注意这里一定是小写的-i,大写的功能是不一样的。结果会先打印response header,然后是空一行,然后是响应结果。
-
Print Only the Response Headers
只打印header不要响应的情况。
Use a Proxy (TLDR: Use -x protocol://host:port argument)
使用代理
Ignore the SSL Certificate (TLDR: Use -k argument)
忽略SSL证书校验啥的
Make Curl Silent (TLDR: Use -s argument)
how to make curl silent so that it doesn't print progress bar, errors, and other output that can get in the way
Save the Response to a File (TLDR: Use -o file argument)
curl -o response.txt https://google.com?q=kittiesMake Curl Slow (TLDR: Use --limit-rate 8k (8KB/sec) argument)
神奇,可以通过--limit-rate来限制curl的速度,也不知道为啥需要这个
Debug Curl Requests (TLDR: Use -v or --trace arguments)
其实不是代码那种debug断点啥的,是把整个过程详细的打印出来
-
curl -v https://www.baidu.com
会把整个过程详细的打印出来;具体怎么看,参考下文:
This recipe uses the
-vargument to make curl print detailed information about the request and the response. Lines prefixed by>is the data sent to the server, lines prefixed by<is the data received from the server, and lines starting with*is misc information, such as connection information, SSL handshake information, and protocol information. -
curl --trace - https://catonmat.net
16进制打印中间详细结果,感觉目前用不到。注意命令比较特殊。
-
Include Response Headers in the Output
-i
注意,如果想看到requestHeader的话,目前似乎只看到通过-v来看的
Make a GET Request (TLDR: No arguments required, it's the default)
默认的curl就是发送GET请求
curl https://catonmat.net参考文献
4 http://www.ruanyifeng.com/blog/2019/09/curl-reference.html
TODO List
| 时间 | 内容 | |
|---|---|---|
总结
| 时间 | 内容 | |
|---|---|---|