当前位置:网站首页>Mock服务moco系列(二)- Json格式、File文件、Header、Cookie、解决中文乱码
Mock服务moco系列(二)- Json格式、File文件、Header、Cookie、解决中文乱码
2022-07-25 17:44:00 【wangmcn】
Mock服务moco系列(二)
Json格式、File文件、Header、Cookie、解决中文乱码
目录
- 1、Json格式
- 2、File文件
- 3、Header
- 4、Cookie
- 5、解决中文乱码
1、Json格式
1、创建04Json.json配置文件。
json为Json格式。
内容如下:
[
{
"description":"Json格式",
"request":{
"uri":"/json",
"method":"get"
},
"response":{
"json":{
"username":"admin",
"password":"123456"
}
}
}
]2、输入启动moco服务命令。
java -jar moco-runner-0.12.0-standalone.jar http -p 8083 -c 04Json.json
3、Postman访问moco服务地址。
打开已安装好的Postman。
协议类型选为GET
访问地址:http://localhost:8083/json
点击Send,访问结果显示:以Json格式输出。
2、File文件
1、创建05File.json、data.json、data2.json配置文件。
请求部分file读取data.json文件做为请求参数。
响应部分file读取data2.json文件做为响应内容。
05File.json内容如下:
[
{
"description":"File文件",
"request":{
"uri":"/file",
"method":"post",
"file":{
"json":"data.json"
}
},
"response":{
"file":"data2.json"
}
}
]data.json内容如下:
{
"username":"admin",
"password":"123456"
}data2.json内容如下:
{
"username":"administrator",
"password":"abcdef"
}2、输入启动moco服务命令。
java -jar moco-runner-0.12.0-standalone.jar http -p 8083 -c 05File.json
3、Postman访问moco服务地址。
打开已安装好的Postman。
协议类型选为POST
访问地址:http://localhost:8083/file
Body添加data.json做为请求参数。
点击Send,访问结果显示:data2.json文件内容为响应内容。
3、Header
1、创建06Header.json配置文件。
headers:信息头。
请求部分headers添加content-type内容类型为Json格式,发送Json格式数据。
响应部分headers添加自定义参数Self-Header与其对应值,响应内容为Json格式。
内容如下:
[
{
"description":"Header",
"request":{
"uri":"/header",
"method":"post",
"headers":{
"content-type":"application/json"
},
"json":{
"username":"admin",
"password":"123456"
}
},
"response":{
"headers":{
"Self-Header":"MySelfHeader"
},
"json":{
"username":"administrator",
"password":"abcdef"
}
}
}
]2、输入启动moco服务命令。
java -jar moco-runner-0.12.0-standalone.jar http -p 8083 -c 06Header.json
3、Postman访问moco服务地址。
打开已安装好的Postman。
协议类型选为POST
访问地址:http://localhost:8083/header
Headers添加键Content-Type,值application/json。
Body添加请求的Json数据。
点击Send,访问结果显示:
Body内容显示以Json格式输出。
Headers显示自定义参数Self-Header与其对应值的信息。
4、Cookie
1、创建07Cookie.json配置文件。
该配置文件有2个接口(有Cookie的Get请求、有Cookie的Post请求)。
cookies:cookie信息。
status:响应状态代码。
内容如下:
[
{
"description":"Cookie(Get请求)",
"request":{
"uri":"/get/cookie",
"method":"get",
"cookies":{
"login":"true"
}
},
"response":{
"text":"Moco Cookie"
}
},
{
"description":"Cookie(Post请求)",
"request":{
"uri":"/post/cookie",
"method":"post",
"cookies":{
"login":"true"
},
"json":{
"username":"admin",
"password":"123456"
}
},
"response":{
"status":200,
"json":{
"admin":"success",
"status":"1"
}
}
}
]2、输入启动moco服务命令。
java -jar moco-runner-0.12.0-standalone.jar http -p 8083 -c 07Cookie.json
3、Postman访问moco服务地址。
打开已安装好的Postman。
(1)Cookie(Get请求)
协议类型选为GET
访问地址:http://localhost:8083/get/cookie
Headers添加键Cookie,值login=true。
点击Send,访问结果显示:以Text格式输出。
(2)Cookie(Post请求)
协议类型选为POST
访问地址:http://localhost:8083/post/cookie
Headers添加键Cookie,值login=true。
Headers添加键Content-Type,值application/json。
Body添加请求的Json数据。
点击Send,访问结果显示:
Body内容显示以Json格式输出。
状态代码为200。
5、解决中文乱码
一、问题:
当响应内容有中文时(如:返回的响应内容),浏览器查看时显示乱码,如图所示:
二、解决:
在配置文件response中添加设置编码格式为“GBK”即可。
"headers":{"Content-Type":"text/html;charset=gbk"}
1、创建08CharsetGBK.json配置文件。
内容如下:
[
{
"description":"解决中文乱码",
"request":{
"uri":"/demo",
"method":"get"
},
"response":{
"headers":{
"Content-Type":"text/html;charset=gbk"
},
"text":"返回的响应内容"
}
}
]2、输入启动moco服务命令。
java -jar moco-runner-0.12.0-standalone.jar http -p 8083 -c 08CharsetGBK.json
3、浏览器访问moco服务地址。
访问地址:http://localhost:8083/demo
访问结果显示:中文显示正常。
边栏推荐
- With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
- WPF implements user avatar selector
- [solution] the Microsoft edge browser has the problem of "unable to access this page"
- Installation steps and usage of NVM under windows10 system
- Redis源码与设计剖析 -- 17.Redis事件处理
- Go defer and recover simple notes
- 03.无重复字符的最长子串
- 【解决方案】Microsoft Edge 浏览器 出现“无法访问该页面”问题
- With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
- 电子产品“使用”和“放置”哪个寿命更长??
猜你喜欢

OSPF comprehensive experiment

I2C通信——时序图

window10系统下nvm的安装步骤以及使用方法

EDI docking commercehub orderstream

绘制pdf表格 (二) 通过itext实现在pdf中绘制excel表格样式设置中文字体、水印、logo、页眉、页码

「数字安全」警惕 NFT的七大骗局

With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development

HCIP第一天实验

绘制pdf表格 (一) 通过itext实现在pdf中绘制excel表格样式并且实现下载(支持中文字体)

EDI 对接CommerceHub OrderStream
随机推荐
对灰度图像的三维函数显示
Thesis reading_ Multi task learning_ MMoE
How to rectify the unqualified EMC of electronic products?
栈的顺序存储结构,链式存储结构及实现
关于flickr的数据集笔记
简述Synchronized以及锁升级
约瑟夫环问题
自动化测试 PO设计模型
WPF 实现用户头像选择器
mysql case when
Google Earth engine - download the globalmlbuildingfootprints vector collection of global buildings
[Hardware Engineer] can't select components?
如何看一本书
面试官:说说 log.Fatal 和 panic 的区别
OSPF综合实验
Ultimate doll 2.0 | cloud native delivery package
Starting from business needs, open the road of efficient IDC operation and maintenance
网上开期货账户安全吗?手续费怎么申请才低?
Pymongo saves data in dataframe format (insert_one, insert_many, multi-threaded saving)
Stm32 paj7620u2 gesture recognition module (IIC communication) program source code explanation