最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

IDEA中的HTTP Client使用教程

 更新時(shí)間:2021年03月04日 11:39:54   投稿:mrr  
這篇文章主要介紹了IDEA中的HTTP Client使用教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

介紹

IDEA RESTful WebServices是一個(gè)類(lèi)似jmeter,postman的工具??梢允褂眉兾谋揪庉?。

官網(wǎng)介紹地址:https://www.jetbrains.com/help/idea/restful-webservices.html

該工具是idea的一個(gè)組件,在Tools->Http client下;當(dāng)然goland也是相同;低版本是Test Restful WebService,新版本的idea已經(jīng)提示改功能廢棄,建議使用new HTTP Client也就是我們此教程要介紹的工具;

示例:

創(chuàng)建demo1.http文件

GET https://www.baidu.com

###

點(diǎn)擊右側(cè)運(yùn)行即可查看到結(jié)果

HTTP請(qǐng)求中使用變量

要在請(qǐng)求中提供變量,請(qǐng)將其括在雙花括號(hào)中,如 {{variable}} 。變量名稱只能包含字母,數(shù)字,下 劃線符號(hào) _ 或連字符 - 。

預(yù)定義的動(dòng)態(tài)變量

每次您運(yùn)行請(qǐng)求時(shí),動(dòng)態(tài)變量都會(huì)生成一個(gè)值: $uuid :生成通用的唯一標(biāo)識(shí)符(UUID-v4) $timestamp :生成當(dāng)前的UNIX時(shí)間戳 $randomInt :生成介于0到1000之間的隨機(jī)整數(shù)。

GET http://localhost/api/get?id={{$uuid}}

創(chuàng)建環(huán)境變量

在項(xiàng)目?jī)?nèi)部,創(chuàng)建以下文件:

  • 在rest-client.env.json(或http-client.env.json)是包含常見(jiàn)的變量,其目的是要與你的項(xiàng)目一起 分發(fā)的常規(guī)文件。
  • 在rest-client.private.env.json(或http-client.private.env.json)是一個(gè) 私人 的文件可能包括密 碼,令牌,證書(shū)和其他敏感信息。默認(rèn)情況下,此文件被添加到VCS忽略文件列表中。在httpclient.private.env.json文件中指定的變量的值將覆蓋環(huán)境文件中的值。
{
 "dev": {
 "host": "http://127.0.0.1:80",
 "name": "zhangsan"
 },
 "prod": {
 "host": "http://127.0.0.1:80",
 "name":"lisi"
 }
}

調(diào)用示例

GET http://{{host}}/api/get?name={{name}}

腳本設(shè)置環(huán)境變量

//設(shè)置環(huán)境變量
> {%
client.global.set("token", response.body.token);
%}

腳本檢測(cè)

可以對(duì)返回值進(jìn)行打印,斷言;

# 登陸
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);
	});
%}

###

類(lèi)型介紹

  • client

client.global

  • set(varName, varValue) // 設(shè)置全局變量
  • get(varName) // 獲取全局變量
  • isEmpty // 檢查 global 是否為空
  • clear(varName) // 刪除變量
  • clearAll // 刪除所有變量
  • client.test(testName, func) // 創(chuàng)建一個(gè)名稱為 testName 的測(cè)試
  • client.assert(condition, message) // 校驗(yàn)條件 condition 是否成立,否則拋出異常 message
  • client.log(text) // 打印日志
  • response
  • response.body // 字符串 或 JSON (如果 content-type 為 application/json .)
  • response.headers

valueOf(headerName) // 返回第一個(gè)匹配 headerName 的值,如果沒(méi)有匹配的返回 null
valuesOf(headerName) // 返回所有匹配 headerName 的值的數(shù)組,如果沒(méi)有匹配的返回空數(shù)組

  • response.status // Http 狀態(tài)碼,如: 200 / 400
  • response.contentType

mimeType // 返回 MIME 類(lèi)型,如: text/plain , text/xml , application/json .
charset // 返回編碼 UTF-8 等

示例test.http

###
# GET請(qǐng)求
GET http://{{host}}/api/get?name={{name}}

###

# POST請(qǐng)求
POST http://{{host}}/api/post/kv
Content-Type: application/x-www-form-urlencoded

name=zhangsan&age=11
###

# POST請(qǐng)求
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);
	});
%}

###

# 登陸后訪問(wèn)用戶信息
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");
	});
%}
###

# 登陸后訪問(wèn)用戶年齡
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")
	// 默認(rèn)路徑
	// GET帶參數(shù)
	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:https://github.com/goflyfox/tools

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

教程視頻

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

到此這篇關(guān)于IDEA中的HTTP Client使用教程的文章就介紹到這了,更多相關(guān)IDEA HTTP Client使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • springboot獲取resources下static目錄的位置

    springboot獲取resources下static目錄的位置

    這篇文章主要為大家詳細(xì)介紹了springboot獲取resources下static目錄的位置的三種常用方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下
    2024-12-12
  • Java簡(jiǎn)單數(shù)組排序(冒泡法)

    Java簡(jiǎn)單數(shù)組排序(冒泡法)

    這篇文章主要介紹了Java簡(jiǎn)單數(shù)組排序,實(shí)例分析了基于冒泡法實(shí)現(xiàn)數(shù)組排序的相關(guān)技巧,簡(jiǎn)單實(shí)用,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • ThreadLocal內(nèi)存泄露的產(chǎn)生原因和處理方法

    ThreadLocal內(nèi)存泄露的產(chǎn)生原因和處理方法

    ThreadLocal 的內(nèi)存泄漏問(wèn)題通常發(fā)生在使用 ThreadLocal 存儲(chǔ)對(duì)象時(shí),尤其是在多線程環(huán)境中,線程池中的線程復(fù)用可能導(dǎo)致一些資源沒(méi)有及時(shí)清理,從而引發(fā)內(nèi)存泄漏,所以本文給大家介紹了ThreadLocal內(nèi)存泄露的產(chǎn)生原因和處理方法,需要的朋友可以參考下
    2024-12-12
  • java小程序火鍋店點(diǎn)餐系統(tǒng)

    java小程序火鍋店點(diǎn)餐系統(tǒng)

    這篇文章主要介紹了java小程序火鍋店點(diǎn)餐系統(tǒng),采用Java語(yǔ)言和Vue技術(shù),以小程序模式實(shí)現(xiàn)的火鍋點(diǎn)菜系統(tǒng),文中提供了解決思路和部分實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2023-03-03
  • java實(shí)現(xiàn)字符串和日期類(lèi)型相互轉(zhuǎn)換的方法

    java實(shí)現(xiàn)字符串和日期類(lèi)型相互轉(zhuǎn)換的方法

    這篇文章主要介紹了java實(shí)現(xiàn)字符串和日期類(lèi)型相互轉(zhuǎn)換的方法,涉及java針對(duì)日期與字符串的轉(zhuǎn)換與運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下
    2017-02-02
  • Spring中Websocket身份驗(yàn)證和授權(quán)的實(shí)現(xiàn)

    Spring中Websocket身份驗(yàn)證和授權(quán)的實(shí)現(xiàn)

    在Web應(yīng)用開(kāi)發(fā)中,安全一直是非常重要的一個(gè)方面,本文主要介紹了Spring中Websocket身份驗(yàn)證和授權(quán)的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-08-08
  • 詳解IDEA下Gradle多模塊(項(xiàng)目)的構(gòu)建

    詳解IDEA下Gradle多模塊(項(xiàng)目)的構(gòu)建

    這篇文章主要介紹了詳解IDEA下Gradle多模塊(項(xiàng)目)的構(gòu)建,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • Java基礎(chǔ)之反射原理與用法詳解

    Java基礎(chǔ)之反射原理與用法詳解

    這篇文章主要介紹了Java基礎(chǔ)之反射原理與用法,結(jié)合實(shí)例形式詳細(xì)分析了java反射的相關(guān)概念、原理、使用方法與操作注意事項(xiàng),需要的朋友可以參考下
    2020-02-02
  • Spring Boot項(xiàng)目中定制攔截器的方法詳解

    Spring Boot項(xiàng)目中定制攔截器的方法詳解

    這篇文章主要介紹了Spring Boot項(xiàng)目中定制攔截器的方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • @Configuration與@Component作為配置類(lèi)的區(qū)別詳解

    @Configuration與@Component作為配置類(lèi)的區(qū)別詳解

    這篇文章主要介紹了@Configuration與@Component作為配置類(lèi)的區(qū)別詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06

最新評(píng)論

无锡市| 肥西县| 太原市| 巧家县| 上思县| 台州市| 凤翔县| 石景山区| 汨罗市| 塘沽区| 左贡县| 青海省| 论坛| 金坛市| 抚宁县| 台中市| 定州市| 买车| 鄂温| 西吉县| 白朗县| 呈贡县| 岗巴县| 大悟县| 莱阳市| 牙克石市| 大英县| 永顺县| 齐齐哈尔市| 荣昌县| 济南市| 屯留县| 渝北区| 吐鲁番市| 丘北县| 惠安县| 长岭县| 汉阴县| 荔浦县| 平遥县| 清水河县|