1.熟悉、梳理、总结项目研发实战中的cURL接口测试常用命令,刚好是最近研发遇到的问题。
2.欢迎点赞、关注、批评、指正,互三走起来,小手动起来!


文章目录

  • 1.`cURL`简要介绍
  • 2. `cURL`常用命令清单
  • 3. 测试命令行
  • 4. 部分效果示例(3个)


1.cURL简要介绍

  • cURL 支持广泛的协议如 HTTP、HTTPSFTP 的,满足几乎所有的网络请求需求。大多数操作系统都内置了命令行工具(Linux、Windows等),方便进行 cURL 测试。
  • cURL,广泛应用于 API 测试和网页内容检查,如获取网站数据、发起 GET/POST 调用、传输文件、自定义请求头等多种操作。

2. cURL常用命令清单



3. 测试命令行

curl https://www.baidu.com/	# 不带有任何参数时,curl 发出 GET 请求

curl -A	# -A参数指定客户端的用户代理标头,即User-Agent

curl -b 'foo=bar' https://google.com	# -b参数用来向服务器发送 Cookie,向服务器发送一个名为foo、值为bar的 Cookie

curl -c cookies.txt https://www.google.com	# 将服务器设置的 Cookie 写入一个文件

curl -d 'login=emma&password=123'-X POST https://google.com/login	# 发送 POST 请求的数据体

curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login	# 添加 HTTP 请求的标头是Content-Type: application/json,然后用-d参数发送 JSON 数据

curl --data-urlencode 'comment=hello world' https://google.com/login	# 发送 POST 请求的数据体,区别在于会自动将发送的数据进行 URL 编码

curl -e 'https://google.com?q=example' https://www.example.com	# 设置 HTTP 的标头Referer,表示请求的来源

curl -H 'Referer: https://google.com?q=example' https://www.example.com	# 通过直接添加标头Referer

curl -F 'file=@photo.png' https://google.com/profile	# 向服务器上传二进制文件

curl -G -d 'q=kitties' -d 'count=20' https://google.com/search	# 构造 URL 的查询字符串,实际请求的 URL 为https://google.com/search?q=kitties&count=20

curl -i https://www.example.com	# 输出服务器回应的标头,然后空一行,再输出网页的源码

curl -I https://www.example.com	# 向服务器发出 HEAD 请求,然会将服务器返回的 HTTP 标头打印出来,--head参数等同于-I

curl -k https://www.example.com	# 不会检查服务器的 SSL 证书是否正确

curl -O https://www.example.com/foo/bar.html	# 将服务器回应保存成文件,并将 URL 的最后部分当作文件名,文件名为bar.html

curl -u 'bob:12345' https://google.com/login	# 设置用户名为bob,密码为12345,然后将其转为 HTTP 标头Authorization: Basic Ym9iOjEyMzQ1

###############################
##############常用#############
curl -H 'content-type: application/json' -X POST -d '{"accountType":"4","channel":"1","channelId":"YW_MMY","uid":"13154897541","phoneNumber":"13154897541","loginSource":"3","loginType":"1","userIp":"192.168.2.3","postTime":"14633fffffffffff81286","userAgent":"Windows NT","imei":"352600051025733","macAddress":"40:92:d4:cb:46:43","serialNumber":"123"}'	

curl -X POST -H 'content-type:application/xml' -d '<?xml version="1.0" encoding="UTF-8"?><name>zhangsan</name>' http://www.baidu.com/	
###############################

curl -H 'Content-Type:application/json' -X POST -d '{"name":"zhangsan"}' http://www.baidu.com/	

	-X/--request [GET|POST|PUT|DELETE|…]	# 使用指定的http method发出 http request
	
	-H/--header	设置请求头。例如,-H "Content-Type: application/json"会将Content-Type设置为application/json,多个header要使用多个-H
	
	-i/--include	# 显示response的header
	
	-d/--data	# 用于发送 POST 请求的数据体,默认为POST请求
	
	-v/--verbose	# 输出比较多的信息,查看请求详情
	
	-u/--user	# 使用者账号
	
	-b/--cookie cookie	# 文件路径 使用cookie
	
	--data-binary	# 发送二进制数据

###############################
##############常用#############
curl -F "file=@/path/to/file" # http://example.com/upload	上传文件

curl 'http://115.28.108.130:5000/api/user/getUserList/' \
-H 'Cookie:PYSESSID=05eac6ba-2eb6-11e9-a130-00163e06e52c;session=eyIwNWVhYzZiYS0yZWI2LTExZTktYTEzMC0wMDE2M2UwNmU1MmMiOnRydWV9.D0Q51A.u9XjrAZb-plUSl01QnNPkGXJPck'	# Cookie属于header的一项,可以携带抓取到的Cookie来请求需要登录的接口

curl -o filename https://apifox.com/file	# 下载文件

curl -c cookies.txt https://apifox.com	# 保存和应用 Cookies
###############################

4. 部分效果示例(3个)

  • 示例【1】
  • 示例【2】
  • 示例【3】-Windows系统测试效果