您好,登錄后才能下訂單哦!
這篇文章主要介紹“IDEA之如何使用HTTP Client”,在日常操作中,相信很多人在IDEA之如何使用HTTP Client問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”IDEA之如何使用HTTP Client”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
IDEA RESTful WebServices是一個(gè)類似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é)果
要在請(qǐng)求中提供變量,請(qǐng)將其括在雙花括號(hào)中,如 {{variable}} 。變量名稱只能包含字母,數(shù)字,下 劃線符號(hào) _ 或連字符 - 。
每次您運(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}}
在項(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)境變量 > {% client.global.set("token", response.body.token); %}
可以對(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); }); %} ###
client
set(varName, varValue) // 設(shè)置全局變量
get(varName) // 獲取全局變量
isEmpty // 檢查 global 是否為空
clear(varName) // 刪除變量
clearAll // 刪除所有變量
client.global
client.test(testName, func) // 創(chuàng)建一個(gè)名稱為 testName
的測(cè)試
client.assert(condition, message) // 校驗(yàn)條件 condition
是否成立,否則拋出異常 message
client.log(text) // 打印日志
response
mimeType // 返回 MIME 類型,如:text/plain
, text/xml
, application/json
.
charset // 返回編碼 UTF-8 等
valueOf(headerName) // 返回第一個(gè)匹配 headerName 的值,如果沒(méi)有匹配的返回 null
valuesOf(headerName) // 返回所有匹配 headerName 的值的數(shù)組,如果沒(méi)有匹配的返回空數(shù)組
response.body // 字符串 或 JSON (如果content-type
為 application/json
.)
response.headers
response.status // Http 狀態(tài)碼,如: 200 / 400
response.contentType
### # 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} ###
### # 未登錄 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"); }); %} ###
{ "dev": { "host": "http://127.0.0.1:80", "name": "zhangsan" }, "prod": { "host": "http://127.0.0.1:80", "name":"lisi" } }
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() }
到此,關(guān)于“IDEA之如何使用HTTP Client”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。