IDEA中的HTTP Client使用教程
介绍
IDEARESTfulWebServices是一个类似jmeter,postman的工具。可以使用纯文本编辑。
官网介绍地址:https://www.jetbrains.com/help/idea/restful-webservices.html
该工具是idea的一个组件,在Tools->Httpclient下;当然goland也是相同;低版本是TestRestfulWebService,新版本的idea已经提示改功能废弃,建议使用newHTTPClient也就是我们此教程要介绍的工具;
示例:
创建demo1.http文件
GEThttps://www.baidu.com
###
点击右侧运行即可查看到结果
HTTP请求中使用变量
要在请求中提供变量,请将其括在双花括号中,如{{variable}}。变量名称只能包含字母,数字,下划线符号_或连字符-。
预定义的动态变量
每次您运行请求时,动态变量都会生成一个值:$uuid:生成通用的唯一标识符(UUID-v4)$timestamp:生成当前的UNIX时间戳$randomInt:生成介于0到1000之间的随机整数。
GEThttp://localhost/api/get?id={{$uuid}}
创建环境变量
在项目内部,创建以下文件:
- 在rest-client.env.json(或http-client.env.json)是包含常见的变量,其目的是要与你的项目一起分发的常规文件。
- 在rest-client.private.env.json(或http-client.private.env.json)是一个私人的文件可能包括密码,令牌,证书和其他敏感信息。默认情况下,此文件被添加到VCS忽略文件列表中。在httpclient.private.env.json文件中指定的变量的值将覆盖环境文件中的值。
{
"dev":{
"host":"http://127.0.0.1:80",
"name":"zhangsan"
},
"prod":{
"host":"http://127.0.0.1:80",
"name":"lisi"
}
}
调用示例
GEThttp://{{host}}/api/get?name={{name}}
脚本设置环境变量
//设置环境变量
>{%
client.global.set("token",response.body.token);
%}
脚本检测
可以对返回值进行打印,断言;
#登陆
POSThttp://{{host}}/system/login
Content-Type:application/x-www-form-urlencoded
username=admin&password=123456
>{%
client.log(JSON.stringify(response.body));
client.test("Requestexecutedsuccessfully",function(){
client.assert(response.status===200,"Responsestatusisnot200");
});
client.test("Responsecontent-typeisjson",function(){
vartype=response.contentType.mimeType;
client.assert(type==="application/json","Expected'application/json'butreceived'"+type+"'");
});
client.test("Requestcodesuccess",function(){
client.assert(response.body.code===0,"Responsecodeisnot0");
client.global.set("token",response.body.data);
});
%}
###
类型介绍
- client
client.global
- set(varName,varValue)//设置全局变量
- get(varName)//获取全局变量
- isEmpty//检查global是否为空
- clear(varName)//删除变量
- clearAll//删除所有变量
- client.test(testName,func)//创建一个名称为testName的测试
- client.assert(condition,message)//校验条件condition是否成立,否则抛出异常message
- client.log(text)//打印日志
- response
- response.body//字符串或JSON(如果content-type为application/json.)
- response.headers
valueOf(headerName)//返回第一个匹配headerName的值,如果没有匹配的返回null
valuesOf(headerName)//返回所有匹配headerName的值的数组,如果没有匹配的返回空数组
- response.status//Http状态码,如:200/400
- response.contentType
mimeType//返回MIME类型,如:text/plain,text/xml,application/json.
charset//返回编码UTF-8等
示例test.http
###
#GET请求
GEThttp://{{host}}/api/get?name={{name}}
###
#POST请求
POSThttp://{{host}}/api/post/kv
Content-Type:application/x-www-form-urlencoded
name=zhangsan&age=11
###
#POST请求
POSThttp://{{host}}/api/post/json
Content-Type:application/json
referer:https://goframe.org/
cookie:name=zhangsan;age=11
{"name":"zhangsan","age":11}
###
test2.http
###
#未登录
POSThttp://{{host}}/system/user/info
>{%
client.log(JSON.stringify(response.body));
client.test("Requestexecutedsuccessfully",function(){
client.assert(response.status===404,"Responsestatusisnot200");
});
client.test("Responsecontent-typeisjson",function(){
vartype=response.contentType.mimeType;
client.assert(type==="application/json","Expected'application/json'butreceived'"+type+"'");
});
client.test("Requestcodefail",function(){
client.assert(response.body.code===-1,"Responsecodeisnot-1");
});
%}
###
#登陆
POSThttp://{{host}}/system/login
Content-Type:application/x-www-form-urlencoded
username=admin&password=123456
>{%
client.log(JSON.stringify(response.body));
client.test("Requestexecutedsuccessfully",function(){
client.assert(response.status===200,"Responsestatusisnot200");
});
client.test("Responsecontent-typeisjson",function(){
vartype=response.contentType.mimeType;
client.assert(type==="application/json","Expected'application/json'butreceived'"+type+"'");
});
client.test("Requestcodesuccess",function(){
client.assert(response.body.code===0,"Responsecodeisnot0");
client.global.set("token",response.body.data);
});
%}
###
#登陆后访问用户信息
POSThttp://{{host}}/system/user/info
token:{{token}}
>{%
client.log(JSON.stringify(response.body));
client.test("Requestexecutedsuccessfully",function(){
client.assert(response.status===200,"Responsestatusisnot200");
});
client.test("Responsecontent-typeisjson",function(){
vartype=response.contentType.mimeType;
client.assert(type==="application/json","Expected'application/json'butreceived'"+type+"'");
});
client.test("Requestcodesuccess",function(){
client.assert(response.body.code===0,"Responsecodeisnot0");
});
%}
###
#登陆后访问用户年龄
POSThttp://{{host}}/system/user/age
token:{{token}}
>{%
client.log(JSON.stringify(response.body));
client.test("Requestexecutedsuccessfully",function(){
client.assert(response.status===200,"Responsestatusisnot200");
});
client.test("Responsecontent-typeisjson",function(){
vartype=response.contentType.mimeType;
client.assert(type==="application/json","Expected'application/json'butreceived'"+type+"'");
});
client.test("Requestcodesuccess",function(){
client.assert(response.body.code===0,"Responsecodeisnot0");
});
%}
###
http-client.env.json
{
"dev":{
"host":"http://127.0.0.1:80",
"name":"zhangsan"
},
"prod":{
"host":"http://127.0.0.1:80",
"name":"lisi"
}
}
main.go
packagemain
import(
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
"github.com/gogf/gf/util/guuid"
)
vartokenstring
funcmain(){
s:=g.Server()
group:=s.Group("/api")
//默认路径
//GET带参数
group.GET("/get",func(r*ghttp.Request){
r.Response.Writeln("HelloWorld!")
r.Response.Writeln("name:",r.GetString("name"))
})
//POSTKV
group.POST("/post/kv",func(r*ghttp.Request){
r.Response.Writeln("func:test")
r.Response.Writeln("name:",r.GetString("name"))
r.Response.Writeln("age:",r.GetInt("age"))
})
//POSTJSON
group.POST("/post/json",func(r*ghttp.Request){
r.Response.Writeln("func:test2")
r.Response.Writeln("name:",r.GetString("name"))
r.Response.Writeln("age:",r.GetString("age"))
h:=r.Header
r.Response.Writeln("referer:",h.Get("referer"))
r.Response.Writeln("cookie:",h.Get("cookie"))
r.Response.Writeln(r.Cookie.Map())
})
//模拟登陆
system:=s.Group("/system")
//登陆接口
system.POST("/login",func(r*ghttp.Request){
if"admin"==r.GetString("username")&&
"123456"==r.GetString("password"){
token=guuid.New().String()
r.Response.WriteJson(g.Map{
"code":0,
"data":token,
})
r.Exit()
}
r.Response.WriteJson(g.Map{
"code":-1,
"data":"",
})
})
//获取用户信息
system.POST("/user/info",func(r*ghttp.Request){
iftoken!=r.Header.Get("token")||token==""{
r.Response.WriteJson(g.Map{
"code":-1,
"data":"",
})
r.Exit()
}
//返回用户信息
r.Response.WriteJson(g.Map{
"code":0,
"data":"zhangsan",
})
})
//获取用户年龄
system.POST("/user/age",func(r*ghttp.Request){
iftoken!=r.Header.Get("token")||token==""{
r.Response.WriteJson(g.Map{
"code":-1,
"data":"",
})
r.Exit()
}
//返回用户信息
r.Response.WriteJson(g.Map{
"code":0,
"data":11,
})
})
s.SetPort(80)
s.Run()
}
代码地址
github:https://github.com/goflyfox/tools
gitee:https://gitee.com/goflyfox/tools
教程视频
bilibili教程地址:https://www.bilibili.com/video/BV12V411f7ab/
到此这篇关于IDEA中的HTTPClient使用教程的文章就介绍到这了,更多相关IDEAHTTPClient使用内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!