一篇文章带你入门使用Linux中的curl命令

curl

1、发送GET请求

 

curl URL

 

curl URL?a=1&b=nihao

2、发送POST请求

 

curl -X POST -d 'a=1&b=nihao' URL

3、发送json格式请求:

 

curl -H "Content-Type: application/json" -X POST -d '{"abc":123,"bcd":"nihao"}' URL

 

curl -H "Content-Type: application/json" -X POST -d @test.json URL

curl通用语法:

curl [options] [URL...]

不带有任何参数时,curl 就是发出 GET 请求。

curl https://www.baidu.com

curl参数

  • -A参数

-A参数指定客户端的用户代理标头,即User-Agent。curl 的默认用户代理字符串是curl/[version]

curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' https://google.com/

上面命令将User-Agent改成 Chrome 浏览器。

  • -b参数

-b参数用来给服务器发送cookie数据

curl -b 'foo1=bar;foo2=bar2' https://google.com/

上面命令发送两个 Cookie

  • -c参数

-c参数将服务器设置的 Cookie 写入一个文件

curl -c cookies.txt https://www.google.com/

上面命令将服务器的 HTTP 回应所设置 Cookie 写入文本文件cookies.txt

  • -d参数

-d参数用于发送 POST 请求的数据体

curl -d 'login=emma&password=123' -X POST https://google.com/login

使用-d参数以后,HTTP 请求会自动加上标头Content-Type : application/x-www-form-urlencoded。并且会自动将请求转为 POST 方法,因此可以省略-X POST。

-d参数可以读取本地文本文件的数据,向服务器发送。

curl -d '@data.txt' https://google.com/login

  • --data-urlencode

--data-urlencode参数等同于-d,发送 POST 请求的数据体,区别在于会自动将发送的数据进行 URL 编码。

curl --data-urlencode 'comment=hello world' https://google.com/login

上面代码中,发送的数据hello world之间有一个空格,需要进行 URL 编码。

  • -e参数

-e参数用来设置 HTTP 的标头Referer,表示请求的来源

curl -e 'https://google.com?q=example' https://www.example.com/

-H参数可以通过直接添加标头Referer,达到同样效果

curl -H 'Referer: https://google.com?q=example' https://www.example.com/

  • -F参数

-F参数用来向服务器上传二进制文件。

curl -F 'file=@photo.png' https://google.com/profile

上面命令会给 HTTP 请求加上标头Content-Type: multipart/form-data,然后将文件photo.png作为file字段上传。

-F参数还可以指定 MIME 类型

curl -F 'file=@photo.png;type=image/png' https://google.com/profile

上面命令指定 MIME 类型为image/png,否则 curl 会把 MIME 类型设为application/octet-stream。

-F参数也可以指定文件名。

curl -F 'file=@photo.png;filename=me.png' https://google.com/profile

上面命令中,原始文件名为photo.png,但是服务器接收到的文件名为me.png

  • -G参数

-G参数用来构造 URL 的查询字符串。

curl -G -d 'q=kitties' -d 'count=20' https://google.com/search

上面命令会发出一个 GET 请求,实际请求的 URL 为https://google.com/search?q=kitties&count=20。如果省略-G,会发出一个 POST 请求

  • -H参数

-H参数添加HTTP请求的头

curl -H 'Accept-Language: en-US' https://google.com/ curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://google.com/

  • -i参数

-i参数打印出服务器回应的 HTTP 标头

curl -i https://www.example.com/

上面命令收到服务器回应后,先输出服务器回应的 HTTP标头,然后空一行,再输出网页的源码

  • -I参数

-I参数向服务器发出 HEAD 请求,然会将服务器返回的 HTTP 标头打印出来。

curl -I https://www.example.com/

-I参数相当于--head

  • -k参数

-k参数指定跳过 SSL 检测。

curl -k https://www.example.com

上面命令不会检查服务器的 SSL 证书是否正确。

  • -L参数

-L参数会让 HTTP 请求跟随服务器的重定向。curl 默认不跟随重定向。

curl -L -d 'tweet=hi' https://api.twitter.com/tweet

  • --limit-rate

--limit-rate用来限制 HTTP 请求和回应的带宽,模拟慢网速的环境

curl --limit-rate 200k https://google.com

上面命令将带宽限制在每秒 200K 字节

  • -o参数

-O参数将服务器回应保存成文件,并将 URL 的最后部分当作文件名

curl -O https://www.example.com/foo/bar.html

上面命令将服务器回应保存成文件,文件名为bar.html。

  • -s参数

-s参数将不输出错误和进度信息

curl -s https://www.example.com/

上面命令一旦发生错误,不会显示错误信息。不发生错误的话,会正常显示运行结果。

  • -S参数

-S参数指定只输出错误信息

curl -S -o /dev/null https://google.com/

  • -u参数

-u参数用来设置服务器认证的用户名和密码

curl -u 'bob:12345' https://google.com/login

命令设置用户名为bob,密码为12345,然后将其转为 HTTP 标头

--trace参数也可以用于调试,还会输出原始的二进制数据

curl --trace - https://www.example.com/

  • -x参数

-x参数指定 HTTP 请求的代理

curl -x socks5://james:cats@myproxy.com:8080 https://www.example.com/

上面命令指定 HTTP 请求通过myproxy.com:8080的 socks5 代理发出。

  • -X参数

-X参数指定 HTTP 请求的方法

curl -X POST https://www.example.com/

可参考:http://t.csdnimg.cn/PzYX1

Linux系统中curl命令用法详解_linux curl-CSDN博客

  • 28
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值