欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

IDEA中的HTTP Client使用教程

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

介紹

IDEA RESTful WebServices是一個類似jmeter,postman的工具。可以使用純文本編輯。

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

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

示例:

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

GET https://www.baidu.com

###

點擊右側(cè)運行即可查看到結(jié)果

HTTP請求中使用變量

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

預定義的動態(tài)變量

每次您運行請求時,動態(tài)變量都會生成一個值: $uuid :生成通用的唯一標識符(UUID-v4) $timestamp :生成當前的UNIX時間戳 $randomInt :生成介于0到1000之間的隨機整數(shù)。

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

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

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

  • 在rest-client.env.json(或http-client.env.json)是包含常見的變量,其目的是要與你的項目一起 分發(fā)的常規(guī)文件。
  • 在rest-client.private.env.json(或http-client.private.env.json)是一個 私人 的文件可能包括密 碼,令牌,證書和其他敏感信息。默認情況下,此文件被添加到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);
%}

腳本檢測

可以對返回值進行打印,斷言;

# 登陸
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) // 設(shè)置全局變量
  • get(varName) // 獲取全局變量
  • isEmpty // 檢查 global 是否為空
  • clear(varName) // 刪除變量
  • clearAll // 刪除所有變量
  • client.test(testName, func) // 創(chuàng)建一個名稱為 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 的值的數(shù)組,如果沒有匹配的返回空數(shù)組

  • response.status // Http 狀態(tài)碼,如: 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帶參數(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java線程池中線程數(shù)量到底是幾

    java線程池中線程數(shù)量到底是幾

    本文主要介紹了java線程池中線程數(shù)量到底是幾,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08
  • java高并發(fā)之線程的基本操作詳解

    java高并發(fā)之線程的基本操作詳解

    本文主要介紹java高并發(fā)線程的基本操作,這里整理詳細的資料來解釋線程的知識,有需要的學習高并發(fā)的朋友可以參考下
    2021-10-10
  • Java Selenium實現(xiàn)多窗口切換的示例代碼

    Java Selenium實現(xiàn)多窗口切換的示例代碼

    這篇文章主要介紹了Java Selenium實現(xiàn)多窗口切換的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • zookeeper節(jié)點類型詳解

    zookeeper節(jié)點類型詳解

    今天小編就為大家分享一篇關(guān)于zookeeper節(jié)點類型詳解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • JAVA版排序算法之快速排序示例

    JAVA版排序算法之快速排序示例

    這篇文章主要介紹了JAVA版排序算法之快速排序,結(jié)合實例形式分析了基于java版的遍歷、遞歸實現(xiàn)快速排序功能的具體步驟與操作技巧,需要的朋友可以參考下
    2017-01-01
  • 如何使用Java 8 中的 Stream 遍歷樹形結(jié)構(gòu)

    如何使用Java 8 中的 Stream 遍歷樹形結(jié)構(gòu)

    這篇文章主要介紹了使用Java 8中的Stream遍歷樹形結(jié)構(gòu),我們可以使用Java8中的Stream流一次性把數(shù)據(jù)查出來,然后通過流式處理,我們一起來看看,代碼實現(xiàn)為了實現(xiàn)簡單,就模擬查看數(shù)據(jù)庫所有數(shù)據(jù)到List里面,需要的朋友可以參考下
    2023-08-08
  • JAVA 靜態(tài)代理模式詳解及實例應(yīng)用

    JAVA 靜態(tài)代理模式詳解及實例應(yīng)用

    這篇文章主要介紹了JAVA 靜態(tài)代理模式詳解及實例應(yīng)用的相關(guān)資料,這里舉例說明java 靜態(tài)代理模式該如何使用,幫助大家學習參考,需要的朋友可以參考下
    2016-11-11
  • 圖解Java排序算法之堆排序

    圖解Java排序算法之堆排序

    這篇文章主要為大家詳細介紹了Java經(jīng)典排序算法之堆排序,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Maven項目更換本地倉庫過程圖解

    Maven項目更換本地倉庫過程圖解

    這篇文章主要介紹了Maven項目更換本地倉庫過程圖解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • Java AQS中ReentrantReadWriteLock讀寫鎖的使用

    Java AQS中ReentrantReadWriteLock讀寫鎖的使用

    ReentrantReadWriteLock稱為讀寫鎖,它提供一個讀鎖,支持多個線程共享同一把鎖。這篇文章主要講解一下ReentrantReadWriteLock的使用和應(yīng)用場景,感興趣的可以了解一下
    2023-02-02

最新評論