本文系转载,原文地址:https://www.cnblogs.com/alfred0311/p/7988648.html

序言

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

模拟GET请求

访问百度,通过GET方法请求

命令格式: curl protocol://address:port/url

curl http://www.baidu.com
curl www.baidu.com
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

模拟POST请求

1、参数直接在header里面

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

命令格式:curl -d "args"  "protocol://address:port/url"

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

{"code":"","charge":false,"msg":"查询成功","result":{"HeWeather5":[{"now":{"hum":"","vis":"","pres":"","pcpn":"","fl":"","tmp":"","cond":{"txt":"多云","code":""},"wind":{"sc":"3-4","spd":"","deg":"","dir":"东风"}},"suggestion":{"uv":{"txt":"紫外线强度较弱,建议出门前涂擦SPF在12-15之间、PA+的防晒护肤品。","brf":"弱"},"cw":{"txt":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。","brf":"较适宜"},"trav":{"txt":"天气较好,但丝毫不会影响您出行的心情。温度适宜又有微风相伴,适宜旅游。","brf":"适宜"},"air":{"txt":"气象条件对空气污染物稀释、扩散和清除无明显影响,易感人群应适当减少室外活动时间。","brf":"中"},"comf":{"txt":"白天不太热也不太冷,风力不大,相信您在这样的天气条件下,应会感到比较清爽和舒适。","brf":"舒适"},"drsg":{"txt":"建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。","brf":"较舒适"},"sport":{"txt":"天气较好,赶快投身大自然参与户外运动,尽情感受运动的快乐吧。","brf":"适宜"},"flu":{"txt":"各项气象条件适宜,无明显降温过程,发生感冒机率较低。","brf":"少发"}},"aqi":{"city":{"no2":"","o3":"","pm25":"","qlty":"良","so2":"","aqi":"","pm10":"","co":""}},"basic":{"city":"东莞","update":{"loc":"2017-12-05 17:52","utc":"2017-12-05 09:52"},"lon":"113.7462616","id":"CN101281601","cnty":"中国","lat":"23.04623795"},"daily_forecast":[{"date":"2017-12-05","pop":"","hum":"","uv":"","vis":"","astro":{"ss":"17:41","mr":"19:32","ms":"08:13","sr":"06:52"},"pres":"","pcpn":"0.0","tmp":{"min":"","max":""},"cond":{"txt_n":"多云","code_n":"","code_d":"","txt_d":"晴间多云"},"wind":{"sc":"微风","spd":"","deg":"","dir":"无持续风向"}},{"date":"2017-12-06","pop":"","hum":"","uv":"","vis":"","astro":{"ss":"17:41","mr":"20:36","ms":"09:16","sr":"06:52"},"pres":"","pcpn":"0.0","tmp":{"min":"","max":""},"cond":{"txt_n":"多云","code_n":"","code_d":"","txt_d":"多云"},"wind":{"sc":"微风","spd":"","deg":"","dir":"无持续风向"}},{"date":"2017-12-07","pop":"","hum":"","uv":"","vis":"","astro":{"ss":"17:41","mr":"21:40","ms":"10:14","sr":"06:53"},"pres":"","pcpn":"0.0","tmp":{"min":"","max":""},"cond":{"txt_n":"多云","code_n":"","code_d":"","txt_d":"晴"},"wind":{"sc":"微风","spd":"","deg":"","dir":"无持续风向"}},{"date":"2017-12-08","pop":"","hum":"","uv":"","vis":"","astro":{"ss":"17:42","mr":"22:42","ms":"11:06","sr":"06:54"},"pres":"","pcpn":"0.0","tmp":{"min":"","max":""},"cond":{"txt_n":"晴","code_n":"","code_d":"","txt_d":"晴"},"wind":{"sc":"微风","spd":"","deg":"","dir":"北风"}},{"date":"2017-12-09","pop":"","hum":"","uv":"","vis":"","astro":{"ss":"17:42","mr":"23:42","ms":"11:53","sr":"06:54"},"pres":"","pcpn":"0.0","tmp":{"min":"","max":""},"cond":{"txt_n":"晴","code_n":"","code_d":"","txt_d":"晴"},"wind":{"sc":"微风","spd":"","deg":"","dir":"无持续风向"}},{"date":"2017-12-10","pop":"","hum":"","uv":"","vis":"","astro":{"ss":"17:42","mr":"09:47","ms":"12:36","sr":"06:55"},"pres":"","pcpn":"0.0","tmp":{"min":"","max":""},"cond":{"txt_n":"晴","code_n":"","code_d":"","txt_d":"晴"},"wind":{"sc":"微风","spd":"","deg":"","dir":"无持续风向"}},{"date":"2017-12-11","pop":"","hum":"","uv":"","vis":"","astro":{"ss":"17:42","mr":"00:38","ms":"13:16","sr":"06:56"},"pres":"","pcpn":"0.0","tmp":{"min":"","max":""},"cond":{"txt_n":"多云","code_n":"","code_d":"","txt_d":"晴"},"wind":{"sc":"微风","spd":"","deg":"","dir":"无持续风向"}}],"hourly_forecast":[{"date":"2017-12-05 19:00","pop":"","hum":"","pres":"","tmp":"","cond":{"txt":"晴间多云","code":""},"wind":{"sc":"微风","spd":"","deg":"","dir":"东北风"}},{"date":"2017-12-05 22:00","pop":"","hum":"","pres":"","tmp":"","cond":{"txt":"晴间多云","code":""},"wind":{"sc":"微风","spd":"","deg":"","dir":"东北风"}},{"date":"2017-12-06 01:00","pop":"","hum":"","pres":"","tmp":"","cond":{"txt":"晴","code":""},"wind":{"sc":"微风","spd":"","deg":"","dir":"东北风"}},{"date":"2017-12-06 04:00","pop":"","hum":"","pres":"","tmp":"","cond":{"txt":"晴间多云","code":""},"wind":{"sc":"微风","spd":"","deg":"","dir":"东北风"}},{"date":"2017-12-06 07:00","pop":"","hum":"","pres":"","tmp":"","cond":{"txt":"晴间多云","code":""},"wind":{"sc":"微风","spd":"","deg":"","dir":"东北风"}},{"date":"2017-12-06 10:00","pop":"","hum":"","pres":"","tmp":"","cond":{"txt":"晴间多云","code":""},"wind":{"sc":"微风","spd":"","deg":"","dir":"东北风"}},{"date":"2017-12-06 13:00","pop":"","hum":"","pres":"","tmp":"","cond":{"txt":"晴间多云","code":""},"wind":{"sc":"微风","spd":"","deg":"","dir":"东北风"}},{"date":"2017-12-06 16:00","pop":"","hum":"","pres":"","tmp":"","cond":{"txt":"晴间多云","code":""},"wind":{"sc":"微风","spd":"","deg":"","dir":"东风"}}],"status":"ok"}]}}

这种方法是参数直接在header里面的

2、json数据直接在body里面,可以通过重定向将输出指定到特定文件

如果将输出指定到文件可以通过重定向进行操作

命令格式:curl -H "Content-Type:application/json" -X POST --data (json.data) URL

curl -H "Content-Type:application/json" -X POST --data '{"message": "sunshine"}' http://localhost:8000/

这种方法是json数据直接在body里面的

更多 curl 用法或参数可使用 man 命令查看,亦可访问 https://curl.haxx.se/

在Linux上利用curl 命令模拟 HTTP GET/POST 请求的更多相关文章

  1. [转]curl 命令模拟 HTTP GET/POST 请求

    在 Linux 操作系统上对后端程序进行测试的时候,需要进行模拟连接或者书写测试脚本 curl 访问百度,通过GET方法请求 命令格式: curl protocol://address:port/ur ...

  2. curl 命令模拟 HTTP GET/POST 请求

    https://www.cnblogs.com/alfred0311/p/7988648.html

  3. Linux curl 命令模拟 POST/GET 请求

    Linux curl 命令模拟 POST/GET 请求   本文链接:https://blog.csdn.net/sunboy_2050/article/details/82156402 curl 命 ...

  4. Linux下利用script命令录制并回放终端会话

    Linux下利用script命令录制并回放终端会话 核心命令 script 和 scriptreplay 录制屏幕 script -t 2>timescript typescript 命令解释: ...

  5. Linux上的free命令详解、swap机制

    Linux上的free命令详解   解释一下Linux上free命令的输出. 下面是free的运行结果,一共有4行.为了方便说明,我加上了列号.这样可以把free的输出看成一个二维数组FO(Free ...

  6. PHP在linux上执行外部命令

    PHP在linux上执行外部命令 一.PHP中调用外部命令介绍二.关于安全问题三.关于超时问题四.关于PHP运行linux环境中命令出现的问题 一.PHP中调用外部命令介绍在PHP中调用外部命令,可以 ...

  7. 在linux上用dd命令实现ghost功能

    ghost和g4l 安装操作系统,速度太慢,整个过程太冗长乏味了. 安装过程中,需要回答若干问题,系统需要安装无数个软件,创建和写入无数的文件.因为涉及到大量的文件定位和读写,速度一定是快不起来的. ...

  8. Apache RocketMQ在linux上的常用命令

    Apache RocketMQ在linux上的常用命令 进入maven安装后的rocketmq的bin目录  1.启动Name Server  2.启动Broker 3.关闭Name Server 4 ...

  9. Linux上通过MySQL命令访问MySQL数据库时常见问题汇总

    Linux上通过mysql命令访问MySQL数据库时常见问题汇总 1)创建登录账号 #创建用户并授权 #允许本地访问 create user 'test'@'localhost' identified ...

随机推荐

  1. 2019-11-25-如何在国内发布-UWP-应用

    原文:2019-11-25-如何在国内发布-UWP-应用 title author date CreateTime categories 如何在国内发布 UWP 应用 lindexi 2019-11- ...

  2. RFC函数的初步使用-同步

    1.由于没有外围系统,采用不同SAP不同client之间进行测试. 首先在A-client搭建需要被调用的RFC函数.在A-client里运行SE37创建函数 在属性页签选择“远程启用的模块” 设定i ...

  3. [笔记] NuGet Warning NU5100 处理

    问题描述 在将 .NET 项目编译成 NUGET 包时,如果项目引用了其它 dll 文件,则可能出现这个警告,如果不处理,Nuget 包中可能无法正确引用所添加的 dll . 处理方式 在项目 .cs ...

  4. Java生鲜电商平台-提现模块的设计与架构

    Java生鲜电商平台-提现模块的设计与架构 补充说明:生鲜电商平台-提现模块的设计与架构,提现功能指的卖家把在平台挣的钱提现到自己的支付宝或者银行卡的一个过程. 功能相对而言不算复杂,有以下几个功能需 ...

  5. 手写instanceof (详解原型链) 和 实现绑定解绑和派发的事件类

    A  instanceof  B    是判断  A  是否继承自B,是返回true,  否返回false 再精确点就是判断B   是否  再  A  的 原型链上, 什么是原型链,举个例子: 我们定 ...

  6. ROW_NUMBER()实现分页

    1. 在数据表基础上面添加一个自增的一列记录行数(虚拟的实际数据库不存在,不会影响数据库结构)的列当然也顺便起一个别名(我这里起了一个rowNum) 2.由于rowNum是一个虚拟的.若直接使用会报' ...

  7. vuejs的导航栏固定

    https://blog.csdn.net/wang1006008051/article/details/78003974 博主文章,超级详细,上面传送们 不过博主的导航栏跳动比较明显,我自己做了修复 ...

  8. 9. [mmc subsystem] host(第三章)——sdhci-pltfm说明

    一.sdhci-pltfm说明 sdhci-pltfm并不是实际某个host的driver. sdhci-pltfm是指在sdhci core的基础上,提供了统一对sdhci_host的必要属性进行解 ...

  9. 磁盘I/O 监控 iostat

    iostat -cdxm 2 5 dm-4 如果没有这个命令,需要安装sysstat 包. Usage: iostat [ options ] [ <interval> [ <cou ...

  10. Ubuntu 18.04通过命令禁用/开启触控板

    Ubuntu下经常遇到无法用快捷键关闭触控板的情况,博主的电脑安装Ubuntu18.04后便出现了该问题. 解决办法: 首先查看输入设备的id,命令行输入: xinput ,插鼠标与不插鼠标时,Tou ...