常用示例

GET

curl 默认使用get请求
-G —get 将post data 放入url并使用get请求

  1. curl 'www.example.com?name=tom&age=14'
  2. curl -G -d 'name=tom&age=14' 'www.example.com' #-G 等同于 -X GET
  3. # 数据 url 编码
  4. curl --data--urlencode -G -d 'name=tom' -d 'age=14' 'www.example.com'
  5. curl -d 'name=tom' -d 'age=14' --get 'www.example.com'

POST

-X POST

使用 -d 会默认发送post请求 Content-Type: application/x-www-form-urlencoded

curl -d 'name=tom&age=14' 'www.example.com' 

# 发送json

 curl -d '{"name": "tom", "age": "14"}' -H 'Content-Type: application/json' www.example.com

如何指定请求方式

  1. -X PUT
  2. -X DELETE

保存响应到文件(可用于文件下载)

-o

curl -o b.html www.baidu.com

显示请求详情

-v 或 —verbose

curl -v www.example.com

跟随重定向

-L

curl -L www.example.com

修改UserAgent

-A 或者 —user-agent

curl -A 'MY-USER-AGENT' www.example.com

添加请求头

-H

curl -H 'User-Agent: curl/7.68.0' -H 'Host: www.example.com' www.example.com

设定Cookie

-b —cookie Send cookies from string/file

curl -v -b 'cookie1=v1;cookie2=v2' www.example.com

#使用文件

curl -b path-to-file www.example.com

文件上传

-F

curl -F 'file=@tom.png' www.example.com

# 设定文件名

curl -F 'file=@tom.png;filename=profile.png' www.example.com

# 设定 MIME 类型

curl -F 'file=@tom.png;type=image/png' www.example.com

设置请求来源 Referer

-e

 curl -e 'www.baidu.com' www.example.com

设置请求用户

-u

curl -u 'tom:password123' www.example.com

限制请求网速

—limit-rate

# 模拟100k的网速
curl --limit-rate 100k www.example.com  

# 模拟 200 byte网速
curl --limit-rate 200b www.example.com

显示错误

-sS

curl -sS www.example.com