IDEA中的HTTP Client使用教程
介绍
idea restful webservices是一个类似jmeter,postman的工具。可以使用纯文本编辑。
官网介绍地址:https://www.jetbrains.com/help/idea/restful-webservices.html
该工具是idea的一个组件,在tools->http client
下;当然goland也是相同;低版本是test restful webservice
,新版本的idea已经提示改功能废弃,建议使用new http client
也就是我们此教程要介绍的工具;
示例:
创建demo1.http
文件
get
###
点击右侧运行即可查看到结果
http请求中使用变量
要在请求中提供变量,请将其括在双花括号中,如 {{variable}} 。变量名称只能包含字母,数字,下 划线符号 _ 或连字符 - 。
预定义的动态变量
每次您运行请求时,动态变量都会生成一个值: $uuid :生成通用的唯一标识符(uuid-v4) $timestamp :生成当前的unix时间戳 $randomint :生成介于0到1000之间的随机整数。
get http://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" } }
调用示例
get http://{{host}}/api/get?name={{name}}
脚本设置环境变量
//设置环境变量 > {% client.global.set("token", response.body.token); %}
脚本检测
可以对返回值进行打印,断言;
# 登陆 post http://{{host}}/system/login content-type: application/x-www-form-urlencoded username=admin&password=123456 > {% client.log(json.stringify(response.body)); client.test("request executed successfully", function() { client.assert(response.status === 200, "response status is not 200"); }); client.test("response content-type is json", function() { var type = response.contenttype.mimetype; client.assert(type === "application/json", "expected 'application/json' but received '" + type + "'"); }); client.test("request code success", function() { client.assert(response.body.code === 0, "response code is not 0"); 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请求 get http://{{host}}/api/get?name={{name}} ### # post请求 post http://{{host}}/api/post/kv content-type: application/x-www-form-urlencoded name=zhangsan&age=11 ### # post请求 post http://{{host}}/api/post/json content-type: application/json referer: https://goframe.org/ cookie: name=zhangsan; age=11 {"name":"zhangsan","age":11} ###
test2.http
### # 未登录 post http://{{host}}/system/user/info > {% client.log(json.stringify(response.body)); client.test("request executed successfully", function() { client.assert(response.status === 404, "response status is not 200"); }); client.test("response content-type is json", function() { var type = response.contenttype.mimetype; client.assert(type === "application/json", "expected 'application/json' but received '" + type + "'"); }); client.test("request code fail", function() { client.assert(response.body.code === -1, "response code is not -1"); }); %} ### # 登陆 post http://{{host}}/system/login content-type: application/x-www-form-urlencoded username=admin&password=123456 > {% client.log(json.stringify(response.body)); client.test("request executed successfully", function() { client.assert(response.status === 200, "response status is not 200"); }); client.test("response content-type is json", function() { var type = response.contenttype.mimetype; client.assert(type === "application/json", "expected 'application/json' but received '" + type + "'"); }); client.test("request code success", function() { client.assert(response.body.code === 0, "response code is not 0"); client.global.set("token", response.body.data); }); %} ### # 登陆后访问用户信息 post http://{{host}}/system/user/info token: {{token}} > {% client.log(json.stringify(response.body)); client.test("request executed successfully", function() { client.assert(response.status === 200, "response status is not 200"); }); client.test("response content-type is json", function() { var type = response.contenttype.mimetype; client.assert(type === "application/json", "expected 'application/json' but received '" + type + "'"); }); client.test("request code success", function() { client.assert(response.body.code === 0, "response code is not 0"); }); %} ### # 登陆后访问用户年龄 post http://{{host}}/system/user/age token: {{token}} > {% client.log(json.stringify(response.body)); client.test("request executed successfully", function() { client.assert(response.status === 200, "response status is not 200"); }); client.test("response content-type is json", function() { var type = response.contenttype.mimetype; client.assert(type === "application/json", "expected 'application/json' but received '" + type + "'"); }); client.test("request code success", function() { client.assert(response.body.code === 0, "response code is not 0"); }); %} ###
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
package main import ( "github.com/gogf/gf/frame/g" "github.com/gogf/gf/net/ghttp" "github.com/gogf/gf/util/guuid" ) var token string func main() { s := g.server() group := s.group("/api") // 默认路径 // get带参数 group.get("/get", func(r *ghttp.request) { r.response.writeln("hello world!") r.response.writeln("name:", r.getstring("name")) }) // post kv 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")) }) // post json 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) { if token != 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) { if token != 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:
gitee:
教程视频
bilibili教程地址:https://www.bilibili.com/video/bv12v411f7ab/
到此这篇关于idea中的http client使用教程的文章就介绍到这了,更多相关idea http client使用内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!