电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

IDEA中的HTTP Client使用教程

瀏覽:153日期:2024-07-22 15:14:11
介紹

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/loginContent-Type: application/x-www-form-urlencodedusername=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 的值,如果沒有匹配的返回 nullvaluesOf(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/kvContent-Type: application/x-www-form-urlencodedname=zhangsan&age=11#### POST請求POST http://{{host}}/api/post/jsonContent-Type: application/jsonreferer: 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/loginContent-Type: application/x-www-form-urlencodedusername=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/infotoken: {{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/agetoken: {{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 mainimport ('github.com/gogf/gf/frame/g''github.com/gogf/gf/net/ghttp''github.com/gogf/gf/util/guuid')var token stringfunc 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 KVgroup.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 JSONgroup.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.Headerr.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:https://github.com/goflyfox/tools

gitee:https://gitee.com/goflyfox/tools

教程視頻

bilibili教程地址:https://www.bilibili.com/video/BV12V411f7ab/

到此這篇關于IDEA中的HTTP Client使用教程的文章就介紹到這了,更多相關IDEA HTTP Client使用內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: IDEA
相關文章:
主站蜘蛛池模板: 耐火浇注料价格-高强高铝-刚玉碳化硅耐磨浇注料厂家【直销】 | 阿米巴企业经营-阿米巴咨询管理-阿米巴企业培训-广东键锋企业管理咨询有限公司 | 烟台游艇培训,威海游艇培训-烟台市邮轮游艇行业协会 | 颚式破碎机,圆锥破碎机,制砂机-新乡市德诚机电制造有限公司 | 罗氏牛血清白蛋白,罗氏己糖激酶-上海嵘崴达实业有限公司 | 全自动过滤器_反冲洗过滤器_自清洗过滤器_量子除垢环_量子环除垢_量子除垢 - 安士睿(北京)过滤设备有限公司 | 一点车讯-汽车网站,每天一点最新车讯! | 天津暖气片厂家_钢制散热器_天津铜铝复合暖气片_维尼罗散热器 | 数显恒温培养摇床-卧式/台式恒温培养摇床|朗越仪器 | 防爆正压柜厂家_防爆配电箱_防爆控制箱_防爆空调_-盛通防爆 | 亚克隆,RNAi干扰检测,miRNA定量检测-上海基屹生物科技有限公司 | 2025黄道吉日查询、吉时查询、老黄历查询平台- 黄道吉日查询网 | 直流大电流电源,燃料电池检漏设备-上海政飞 | 风化石头制砂机_方解石制砂机_瓷砖石子制砂机_华盛铭厂家 | 真空吸污车_高压清洗车厂家-程力专用汽车股份有限公司官网 | 龙门加工中心-数控龙门加工中心厂家价格-山东海特数控机床有限公司_龙门加工中心-数控龙门加工中心厂家价格-山东海特数控机床有限公司 | 大流量卧式砂磨机_强力分散机_双行星双动力混合机_同心双轴搅拌机-莱州市龙跃化工机械有限公司 | 爱佩恒温恒湿测试箱|高低温实验箱|高低温冲击试验箱|冷热冲击试验箱-您身边的模拟环境试验设备技术专家-合作热线:400-6727-800-广东爱佩试验设备有限公司 | 置顶式搅拌器-优莱博化学防爆冰箱-磁驱搅拌器-天津市布鲁克科技有限公司 | 定时排水阀/排气阀-仪表三通旋塞阀-直角式脉冲电磁阀-永嘉良科阀门有限公司 | BAUER减速机|ROSSI-MERSEN熔断器-APTECH调压阀-上海爱泽工业设备有限公司 | 环氧树脂地坪_防静电地坪漆_环氧地坪漆涂料厂家-地壹涂料地坪漆 环球电气之家-中国专业电气电子产品行业服务网站! | 安徽合肥格力空调专卖店_格力中央空调_格力空调总经销公司代理-皖格制冷设备 | 压砖机_电动螺旋压力机_粉末成型压力机_郑州华隆机械tel_0371-60121717 | 郑州外墙清洗_郑州玻璃幕墙清洗_郑州开荒保洁-河南三恒清洗服务有限公司 | 哲力实业_专注汽车涂料汽车漆研发生产_汽车漆|修补油漆品牌厂家 长沙一级消防工程公司_智能化弱电_机电安装_亮化工程专业施工承包_湖南公共安全工程有限公司 | 软文发布平台 - 云软媒网络软文直编发布营销推广平台 | 高压分散机(高压细胞破碎仪)百科-北京天恩瀚拓 | 安规_综合测试仪,电器安全性能综合测试仪,低压母线槽安规综合测试仪-青岛合众电子有限公司 | 定时排水阀/排气阀-仪表三通旋塞阀-直角式脉冲电磁阀-永嘉良科阀门有限公司 | 招商帮-一站式网络营销服务|搜索营销推广|信息流推广|短视视频营销推广|互联网整合营销|网络推广代运营|招商帮企业招商好帮手 | 交通信号灯生产厂家_红绿灯厂家_电子警察监控杆_标志杆厂家-沃霖电子科技 | 凝胶成像系统(wb成像系统)百科-上海嘉鹏 | 污水提升器,污水提升泵,地下室排水,增压泵,雨水泵,智能供排水控制器-上海智流泵业有限公司 | 东莞工厂厂房装修_无尘车间施工_钢结构工程安装-广东集景建筑装饰设计工程有限公司 | X光检测仪_食品金属异物检测机_X射线检测设备_微现检测 | 卓能JOINTLEAN端子连接器厂家-专业提供PCB接线端子|轨道式端子|重载连接器|欧式连接器等电气连接产品和服务 | 精密模具-双色注塑模具加工-深圳铭洋宇通 | 北京租车牌|京牌指标租赁|小客车指标出租 | 创绿家招商加盟网-除甲醛加盟-甲醛治理加盟-室内除甲醛加盟-创绿家招商官网 | 上海电子秤厂家,电子秤厂家价格,上海吊秤厂家,吊秤供应价格-上海佳宜电子科技有限公司 |