loadrunner写接口脚本一些注意的事项
一、字符格式(gbk转utf8,utf8转gbk)转换:
//将aa转为GBK
lr_convert_string_encoding(lr_eval_string("{aa}"),"utf-8","gb2312","str");
lr_output_message(lr_eval_string("{str}"));
//将GBK变成UTF-8,供服务器和loadruner使用,使用{a1}就好
lr_convert_string_encoding(lr_eval_string("我是邓绍克啊"),"gb2312","utf-8","a1");
二、web_custom_request 做的post 请求,并做检查点
//首先调用web_reg_find()这个注册函数,假设我们接口的正常返回结果是<Code>0</Code>
web_reg_find("Search=Body", //定义查找范围
"SaveCount=result", //定义查找计数变量名称
"Text=<Code>0</Code>", //定义查找内容
LAST);
web_reg_save_param("message",
"LB=\"result\":",
"RB=",
LAST);
1.json数据格式
// 发送post请求这一部分就可以了。body中是json串,所有“ 要做转义 ;如果使用特殊的header 单写就成了
web_custom_request("loan",
"URL=http://XXXXXXX:XXXX/match-admin-api/rest/api/personToMatch/saveLoan",
"Method=POST", //请求方式
"Mode=HTML",
"EncType=application/json", //指定请求头的Content-Type,这里是JSON
"RecContentType=application/json",
"Body={\"debtCode\":\"12341006FYD9000682016092288\",\"loanAmt\":\"618500\",\"loanPeriod\":\"3\",\"serviceFee\":\"0.03083334\",\"manageFee\":\"0.001\",\"signTime\":\"2016-09-22\",\"bookRate\":\"0.078\",\"repayType\":\"3\",\"loanType\":\"1\",\"borrowerName\":\"AAAA\",\"idCard\":\"430722198701153054\",\"seqNo\":\"1474528206458\",\"sourceType\":\"1\"}",
LAST);
web_custom_request("order",
"URL=http://XXXXXXX:XXXX/match-admin-api/rest/api/creditToMatch/saveOrder",
"Method=POST", //请求方式
"Mode=HTML",
"EncType=application/json", //指定响应头的Content-Type,这里是JSON
"RecContentType=application/json",
"Body={\"orders\":[{\"customerNo\":\"169\",\"endTime\":\"2016-10-27\",\"idCard\":\"420103197505151221\",\"orderAmount\":\"900.0\",\"orderNo\":\"{orderCode}\",\"orderPeriod\":\"1\",\"orderRate\":\"0.06\",\"orderType\":\"1\",\"projectNo\":\"FHJHB02016100130\",\"realName\":\"aaaaa\",\"repayMode\":\"2\",\"startTime\":\"2016-09-25\"}],\"serialNumber\":\"1327faee05104ab1b1995980a5196a67\"}",
LAST);
lr_convert_string_encoding(lr_eval_string("{message}"),"utf-8","gb2312","str");
lr_output_message(lr_eval_string("{str}"));
如果json串都格式都正常,仍然报404错误,检查json串中是否有中文。要将中文先保存到参数中,再做使用就可以了(如示例中的readName, 上面的aaaa用{real_name}替换就行)
lr_convert_string_encoding(
"邓真帅",
LR_ENC_SYSTEM_LOCALE,
LR_ENC_UTF8,
"real_name"
);
2. form-urlencoded 格式
body参数的写法跟josn格式的请示不一样
web_reg_save_param("message",
"LB=\"result\":",
"RB=",
LAST);
web_custom_request("Android_sanbiao",
"URL=https://XXXXXXX:XXXX/rest/api/scatteredProduct/phone/showScatteredProductListByPage",
"Method=POST",
"Resource=0",
"RecContentType=application/x-www-form-urlencoded",
"Referer=",
"Mode=HTML",
"EncType=application/x-www-form-urlencoded",
"Body=pageNum=1&requestSource=android&versionCode=321&pageSize=10",
LAST);
lr_convert_string_encoding(lr_eval_string("{message}"),"utf-8","gb2312","str");
lr_output_message(lr_eval_string("{str}"));
return 0;