IDEA之HTTP Client使用教程
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 https://www.baidu.com ###
点击右侧运行即可查看到结果
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) // 打印日志
- client.global
-
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 等
- mimeType // 返回 MIME 类型,如:
- response.body // 字符串 或 JSON (如果
示例
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() }
代码地址
教程视频
- bilibili教程地址:https://www.bilibili.com/video/BV12V411f7ab/
- 公众号搜索:GoWeb学习之路

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
JDK动态代理:不仅要学会用,更要掌握其原理
微信搜索:码农StayUp主页地址:https://gozhuyinglong.github.io源码分享:https://github.com/gozhuyinglong/blog-demos JDK动态代理是指:代理类实例在程序运行时,由JVM根据反射机制动态的生成。也就是说代理类不是用户自己定义的,而是由JVM生成的。 由于其原理是通过Java反射机制实现的,所以在学习前,要对反射机制有一定的了解。传送门:Java反射机制:跟着代码学反射 下面是本篇讲述内容: 1. JDK动态代理的核心类 JDK动态代理有两大核心类,它们都在Java的反射包下(java.lang.reflect),分别为InvocationHandler接口和Proxy类。 1.1 InvocationHandler接口 代理实例的调用处理器需要实现InvocationHandler接口,并且每个代理实例都有一个关联的调用处理器。当一个方法在代理实例上被调用时,这个方法调用将被编码并分派到其调用处理器的invoke方法上。 也就是说,我们创建的每一个代理实例都要有一个关联的InvocationHandler,并...
- 下一篇
TextMate 2.0.19 发布,macOS 文本编辑器
TextMate 是一个功能强大且可自定义的文本编辑器,支持大量编程语言,并作为开源项目而开发。近日,TextMate 更新至 2.0.19 版本,2021 年 2 月共更新了 4 个版本。 自 2.0.15 版本以来的更新内容如下: 确保 ⌘Z/⇧⌘Z 操作在没有焦点的时候不会被发送到文件浏览器; 如果当前限制低于2048,则增加最大打开文件数; 修复在 macOS 10.14 及更早的版本上无法调整 bundle 编辑器中分割视图大小的问题; 在解决上述问题的同时,删除了 bundle 编辑器的抽屉; 由于缺少链接器选项,2.0.17 以前的版本无法在 macOS 10.14 和更早的系统上启动; TextMate 现在是一个 Universal 软件; 在 Big Sur 上进行了一些调整,改善了外观; 随着在文件浏览器中的聚焦。⌥⌘C 将复制选择作为路径名; 文件浏览器的 context 菜单现在可以显示大多数项目的快捷键,不过大多数项目只有在文件浏览器中聚焦时才能使用; 将文件复制到剪贴板后,现在可以在文件浏览器中 "创建链接"; Atomic saving 现在默认为禁用。...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- CentOS7,CentOS8安装Elasticsearch6.8.6
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS8安装Docker,最新的服务器搭配容器使用
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- CentOS6,CentOS7官方镜像安装Oracle11G
- CentOS7设置SWAP分区,小内存服务器的救世主
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- SpringBoot2整合Thymeleaf,官方推荐html解决方案