LoadRunner----三种post接口
LoadRunner----三种post接口
一、参数为k=v的post接口
Action()
{
//添加关联事务取出接口返回的message关键字的值,存储到msg中
web_reg_save_param("msg",
"LB=message\":\"",
"RB=\"",
"Ord=1",
LAST);
//添加事务
lr_start_transaction("get");
//发起请求
web_url("get",
"URL=http://localhost:8080/pinter/com/getSku?id={num}",
LAST );
//先转换msg为一个c语言的参数,然后使用strmp函数和success进行比较
//如果==0,请求成功,事务检查通过;否则,请求失败,事务检查失败;
if(strcmp(lr_eval_string("{msg}"),"success")==0){
lr_end_transaction("get", LR_PASS);
}else{
lr_end_transaction("get", LR_FAIL);
}
return 0;
}
运行结果如下~
二、参数为json的post接口
Action()
{
web_reg_save_param("fromMsg",
"LB=message\":\"",
"RB=\"",
"Ord=1",
LAST);
web_add_header("Content-type","application/json");
lr_start_transaction("post_json");
web_custom_request("post_json", "Method=POST",
"URL=http://localhost:8080/pinter/com/register",
"Body={\"userName\":\"test\",\"password\":\"1234\",\"gender\":1,\"phoneNum\":\"110\",\"email\":\"[email protected]\",\"address\":\"Beijing\"}",
LAST );
//先对关联函数保存下来的乱码fromMsg转化为C语言函数
//然后把它从utf-8编码转换为本地支持的system-locale编码
// 最终将转后的非乱码数据保存到一个toMsg参数里,这个参数是web参数
lr_convert_string_encoding(lr_eval_string("{fromMsg}"), LR_ENC_UTF8 , LR_ENC_SYSTEM_LOCALE,"toMsg");
if(strcmp(lr_eval_string("{toMsg}"),"注册成功")==0){
lr_end_transaction("post_json", LR_PASS);
}else{
lr_end_transaction("post_json", LR_FAIL);
}
return 0;
}
运行结果如下~
三、参数为k=json的post接口
Action()
{
web_custom_request("post_k_json", "Method=POST",
"URL=http://localhost:8080/pinter/com/buy",
"Body=param={\"skuId\":123,\"num\":10}",
LAST );
return 0;
}
运行结果如下~