詳解IntelliJ IDEA 自帶的 HTTP Client 接口調(diào)用插件吊打 Postman
Java公眾號(hào)【Java技術(shù)迷】一個(gè)在互聯(lián)網(wǎng)領(lǐng)先地位,微信搜索【Java技術(shù)迷】第一時(shí)間閱讀最新文章,通過(guò)下面下載鏈接,即可獲得我精心整理的技術(shù)資料,電子書(shū)籍,一線大廠面試資料和優(yōu)秀簡(jiǎn)歷模板。
http://xiazai.jb51.net/202105/yuanma/javadzsh_jb51.rar
1 前言
當(dāng)我們?cè)陂_(kāi)發(fā)調(diào)試 Web 服務(wù)的時(shí)候,需要對(duì)接口進(jìn)行調(diào)用測(cè)試;或者對(duì)接第三方系統(tǒng)時(shí),需要調(diào)用遠(yuǎn)程第三方的接口進(jìn)行聯(lián)調(diào)。這時(shí),相信大家首選的工具一般會(huì)是 Postman
,一款當(dāng)今比較流行而且功能齊全的接口調(diào)用調(diào)試工具。如下所示:
不過(guò)我們一般使用 IntelliJ IDEA
代碼編輯器來(lái)開(kāi)發(fā)和調(diào)試 Web 服務(wù),如果使用 Postman 工具來(lái)測(cè)試接口,不僅要在電腦上安裝 Postman ,還需要在不同工具之間切換,比較麻煩。幸運(yùn)地的是 IDEA 自帶了一款簡(jiǎn)潔輕量級(jí)的接口調(diào)用插件,HTTP Client
。
2 HTTP Client
HTTP Client 是 IDEA 自帶的一款簡(jiǎn)潔輕量級(jí)的接口調(diào)用插件,通過(guò)它,我們能在 IDEA 上開(kāi)發(fā),調(diào)試,測(cè)試
RESTful Web 服務(wù)
。
注意:確保 HTTP Client 插件是安裝啟動(dòng)的,默認(rèn)是已安裝啟動(dòng)的。若沒(méi)有安裝,在 File - Settings - Plugins 路徑下進(jìn)行安裝,如下:
2.1 創(chuàng)建 HTTP Client 文件
可以創(chuàng)建2種文件類型的 HTTP Client 文件,一種是臨時(shí)文件(scratch files,不跟項(xiàng)目工程掛鉤),一種是非臨時(shí)文件(physical files,跟項(xiàng)目工程掛鉤)。
如果你想創(chuàng)建的 HTTP Client 文件是為了臨時(shí)調(diào)用接口測(cè)試用的,不需要保留記錄供以后使用,則可以使用臨時(shí)文件;如果想存檔記錄請(qǐng)求參數(shù),請(qǐng)求結(jié)果等,后續(xù)再繼續(xù)使用,或者隨項(xiàng)目提交到遠(yuǎn)程git倉(cāng)庫(kù),則建議使用非臨時(shí)文件。
創(chuàng)建 HTTP Client 臨時(shí)文件
打開(kāi)后,顯示界面和Postman差不多,不過(guò)這種風(fēng)格的界面被棄用了,官方不推薦我們使用,在最新版本的 IDEA 中已經(jīng)沒(méi)有這個(gè)界面了。
官方推薦我們使用編碼式的風(fēng)格界面,點(diǎn)擊上個(gè)界面頂部的 Convert request to the new format
,即可打開(kāi)新的 HTTP Client 界面。
創(chuàng)建 HTTP Client 非臨時(shí)文件
可以在項(xiàng)目根目錄下創(chuàng)建一個(gè)存儲(chǔ)請(qǐng)求文件的文件夾,然后在里面創(chuàng)建 HTTP Client 請(qǐng)求文件,如下:
2.2 HTTP Client 特性
HTTP 請(qǐng)求存儲(chǔ)在以.http
或.rest
為后綴的文件中,并且?guī)в?API
小圖標(biāo)。
請(qǐng)求文件可以包含多個(gè)請(qǐng)求,多個(gè)請(qǐng)求中間用3個(gè)井號(hào) ###
隔開(kāi);如果是臨時(shí)文件,每次執(zhí)行請(qǐng)求后,會(huì)在請(qǐng)求下方生成對(duì)應(yīng)請(qǐng)求結(jié)果的文件鏈接,按住 Ctrl + 鼠標(biāo)左鍵
可以打開(kāi)。
所有的請(qǐng)求結(jié)果,請(qǐng)求歷史記錄,cookies等信息會(huì)存放在 .idea
文件夾下,如下:
2.3 如何創(chuàng)建請(qǐng)求
使用右上角的快捷按鈕創(chuàng)建請(qǐng)求,可以選擇不同方式的請(qǐng)求,如下:
使用快捷鍵進(jìn)行創(chuàng)建請(qǐng)求,例如輸入 gtr
可以快速創(chuàng)建一個(gè)簡(jiǎn)單的 GET 請(qǐng)求,如下:
使用 Ctrl + J
快捷鍵可以查看創(chuàng)建 HTTP 請(qǐng)求的所有快捷鍵,如下:
通過(guò) cURL
創(chuàng)建請(qǐng)求,點(diǎn)擊右上角的 Convert form cURL
按鈕,然后輸入 cURL 地址即可自動(dòng)轉(zhuǎn)換,如下:
2.4 請(qǐng)求方式
GET
### GET request with a header GET https://httpbin.org/ip Accept: application/json ### GET request with parameter GET https://httpbin.org/get?show_env=1 Accept: application/json ### GET request with environment variables GET {{host}}/get?show_env={{show_env}} Accept: application/json ### GET request with disabled redirects # @no-redirect GET http://httpbin.org/status/301 ### GET request with dynamic variables GET http://httpbin.org/anything?id={{$uuid}}&ts={{$timestamp}} ###
POST
### Send POST request with json body POST https://httpbin.org/post Content-Type: application/json { "id": 999, "value": "content" } ### Send POST request with body as parameters POST https://httpbin.org/post Content-Type: application/x-www-form-urlencoded id=999&value=content ### Send a form with the text and file fields POST https://httpbin.org/post Content-Type: multipart/form-data; boundary=WebAppBoundary --WebAppBoundary Content-Disposition: form-data; name="element-name" Content-Type: text/plain Name --WebAppBoundary Content-Disposition: form-data; name="data"; filename="data.json" Content-Type: application/json < ./request-form-data.json --WebAppBoundary-- ### Send request with dynamic variables in request's body POST https://httpbin.org/post Content-Type: application/json { "id": {{$uuid}}, "price": {{$randomInt}}, "ts": {{$timestamp}}, "value": "content" } ###
PUT
PUT http://localhost:8080/person/put Content-Type: application/json {"name": "陳皮","age": 17}
PATCH
### PATCH http://localhost:8080/person/put Content-Type: application/json {"name": "陳皮","age": 17}
鑒權(quán)方式
### Basic authorization. GET https://httpbin.org/basic-auth/user/passwd Authorization: Basic user passwd ### Basic authorization with variables. GET https://httpbin.org/basic-auth/user/passwd Authorization: Basic {{username}} {{password}} ### Digest authorization. GET https://httpbin.org/digest-auth/realm/user/passwd Authorization: Digest user passwd ### Digest authorization with variables. GET https://httpbin.org/digest-auth/realm/user/passwd Authorization: Digest {{username}} {{password}} ### Authorization by token, part 1. Retrieve and save token. POST https://httpbin.org/post Content-Type: application/json { "token": "my-secret-token" } > {% client.global.set("auth_token", response.body.json.token); %} ### Authorization by token, part 2. Use token to authorize. GET https://httpbin.org/headers Authorization: Bearer {{auth_token}} ###
斷言方式
### Successful test: check response status is 200 GET https://httpbin.org/status/200 > {% client.test("Request executed successfully", function() { client.assert(response.status === 200, "Response status is not 200"); }); %} ### Failed test: check response status is 200 GET https://httpbin.org/status/404 > {% client.test("Request executed successfully", function() { client.assert(response.status === 200, "Response status is not 200"); }); %} ### Check response status and content-type GET https://httpbin.org/get > {% 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 + "'"); }); %} ### Check response body GET https://httpbin.org/get > {% client.test("Headers option exists", function() { client.assert(response.body.hasOwnProperty("headers"), "Cannot find 'headers' option in response"); }); %} ###
以上就是IntelliJ IDEA 自帶的 HTTP Client 接口調(diào)用插件吊打 Postman的詳細(xì)內(nèi)容,更多關(guān)于idea HTTP Client插件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
mybatis xml如何使用not in 某個(gè)集合的格式
這篇文章主要介紹了mybatis xml如何使用not in 某個(gè)集合的格式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01SpringBoot開(kāi)發(fā)案例之打造私有云網(wǎng)盤(pán)的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot開(kāi)發(fā)案例之打造私有云網(wǎng)盤(pán)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04SpringBoot查詢數(shù)據(jù)庫(kù)導(dǎo)出報(bào)表文件方式
這篇文章主要介紹了SpringBoot查詢數(shù)據(jù)庫(kù)導(dǎo)出報(bào)表文件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-045分鐘快速學(xué)會(huì)spring boot整合JdbcTemplate的方法
這篇文章主要給大家介紹了如何通過(guò)5分鐘快速學(xué)會(huì)spring boot整合JdbcTemplate的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring boot整合JdbcTemplate具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12基于java實(shí)現(xiàn)租車管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了基于java實(shí)現(xiàn)租車管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12Java 實(shí)現(xiàn)倒計(jì)時(shí)功能(由秒計(jì)算天、小時(shí)、分鐘、秒)
最近做項(xiàng)目遇到這樣的需求,天、小時(shí)、分鐘、秒的數(shù)值都是隔開(kāi)的,服務(wù)器端只返回一個(gè)時(shí)間戳長(zhǎng)度,怎么實(shí)現(xiàn)這樣的功能呢?下面小編給大家?guī)?lái)了Java 實(shí)現(xiàn)倒計(jì)時(shí)功能的方案,需要的朋友參考下吧2018-01-01