Loading

Linux-curl命令

使用场景

在 Linux 操作系统上对接口进行测试的时候,需要进行模拟连接或者书写测试脚本时,就需要用到这个实用的curl 命令了。

模拟HTTP

发送GET请求

语法:

curl protocol://ip:port/url

例子:

curl http://www.baidu.com

curl http://poetry.apiopen.top/poetryAuthor?count=2&page=1&name=李白

注意

  1. curl中直接添加&符号不认识问题:在控制台&符号被当做命令行指令运行了,添加转义即可.
curl http://poetry.apiopen.top/poetryAuthor?count=2\&page=1\&name=李白

发送POST请求

请求参数在header中传递

需求:获取天气数据,通过POST方法请求

命令格式:

curl -d "args"  "protocol://address:port/url"

请求示例:

curl -d "city=dongguan&appkey=8010132dcf54491a4eaa387f4db61774" "https://way.jd.com/he/freeweather"

请求参数在body中传递

需求:获取新闻,返回2条数据

命令格式:

curl -H "Content-Type:application/json" -X POST --data '{key:value}' protocol://address:port/url

请求示例:

curl -H "Content-Type:application/json" -X POST --data '{"count": 2}' https://api.apiopen.top/getWangYiNews

模拟HTTPS请求

有的时候curl一个https地址会出现一个不安全的警告,我们可以在请求地址前面加一个--insecure或者-k

比如:

curl -k https://localhost

参考资源

posted @ 2020-03-05 14:36  ^Mao^  阅读(255)  评论(0编辑  收藏  举报