久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

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

詳解IntelliJ IDEA 自帶的 HTTP Client 接口調用插件吊打 Postman

瀏覽:126日期:2024-07-16 10:07:59
目錄1 前言2 HTTP Client2.1 創(chuàng)建 HTTP Client 文件2.2 HTTP Client 特性2.3 如何創(chuàng)建請求2.4 請求方式

Java公眾號【Java技術迷】一個在互聯(lián)網領先地位,微信搜索【Java技術迷】第一時間閱讀最新文章,通過下面下載鏈接,即可獲得我精心整理的技術資料,電子書籍,一線大廠面試資料和優(yōu)秀簡歷模板。

http://xiazai.jb51.net/202105/yuanma/javadzsh_jb51.rar

1 前言

當我們在開發(fā)調試 Web 服務的時候,需要對接口進行調用測試;或者對接第三方系統(tǒng)時,需要調用遠程第三方的接口進行聯(lián)調。這時,相信大家首選的工具一般會是 Postman ,一款當今比較流行而且功能齊全的接口調用調試工具。如下所示:

詳解IntelliJ IDEA 自帶的 HTTP Client 接口調用插件吊打 Postman

不過我們一般使用 IntelliJ IDEA 代碼編輯器來開發(fā)和調試 Web 服務,如果使用 Postman 工具來測試接口,不僅要在電腦上安裝 Postman ,還需要在不同工具之間切換,比較麻煩。幸運地的是 IDEA 自帶了一款簡潔輕量級的接口調用插件,HTTP Client。

詳解IntelliJ IDEA 自帶的 HTTP Client 接口調用插件吊打 Postman

2 HTTP Client

HTTP Client 是 IDEA 自帶的一款簡潔輕量級的接口調用插件,通過它,我們能在 IDEA 上開發(fā),調試,測試 RESTful Web 服務。

注意:確保 HTTP Client 插件是安裝啟動的,默認是已安裝啟動的。若沒有安裝,在 File - Settings - Plugins 路徑下進行安裝,如下:

詳解IntelliJ IDEA 自帶的 HTTP Client 接口調用插件吊打 Postman

2.1 創(chuàng)建 HTTP Client 文件

可以創(chuàng)建2種文件類型的 HTTP Client 文件,一種是臨時文件(scratch files,不跟項目工程掛鉤),一種是非臨時文件(physical files,跟項目工程掛鉤)。

如果你想創(chuàng)建的 HTTP Client 文件是為了臨時調用接口測試用的,不需要保留記錄供以后使用,則可以使用臨時文件;如果想存檔記錄請求參數,請求結果等,后續(xù)再繼續(xù)使用,或者隨項目提交到遠程git倉庫,則建議使用非臨時文件。

創(chuàng)建 HTTP Client 臨時文件

詳解IntelliJ IDEA 自帶的 HTTP Client 接口調用插件吊打 Postman

打開后,顯示界面和Postman差不多,不過這種風格的界面被棄用了,官方不推薦我們使用,在最新版本的 IDEA 中已經沒有這個界面了。

詳解IntelliJ IDEA 自帶的 HTTP Client 接口調用插件吊打 Postman

官方推薦我們使用編碼式的風格界面,點擊上個界面頂部的 Convert request to the new format ,即可打開新的 HTTP Client 界面。

詳解IntelliJ IDEA 自帶的 HTTP Client 接口調用插件吊打 Postman

創(chuàng)建 HTTP Client 非臨時文件

可以在項目根目錄下創(chuàng)建一個存儲請求文件的文件夾,然后在里面創(chuàng)建 HTTP Client 請求文件,如下:

詳解IntelliJ IDEA 自帶的 HTTP Client 接口調用插件吊打 Postman

2.2 HTTP Client 特性

HTTP 請求存儲在以.http或.rest為后綴的文件中,并且?guī)в?API 小圖標。

詳解IntelliJ IDEA 自帶的 HTTP Client 接口調用插件吊打 Postman

請求文件可以包含多個請求,多個請求中間用3個井號 ### 隔開;如果是臨時文件,每次執(zhí)行請求后,會在請求下方生成對應請求結果的文件鏈接,按住 Ctrl + 鼠標左鍵可以打開。

詳解IntelliJ IDEA 自帶的 HTTP Client 接口調用插件吊打 Postman

所有的請求結果,請求歷史記錄,cookies等信息會存放在 .idea 文件夾下,如下:

詳解IntelliJ IDEA 自帶的 HTTP Client 接口調用插件吊打 Postman

2.3 如何創(chuàng)建請求

使用右上角的快捷按鈕創(chuàng)建請求,可以選擇不同方式的請求,如下:

詳解IntelliJ IDEA 自帶的 HTTP Client 接口調用插件吊打 Postman

使用快捷鍵進行創(chuàng)建請求,例如輸入 gtr 可以快速創(chuàng)建一個簡單的 GET 請求,如下:

詳解IntelliJ IDEA 自帶的 HTTP Client 接口調用插件吊打 Postman

使用 Ctrl + J 快捷鍵可以查看創(chuàng)建 HTTP 請求的所有快捷鍵,如下:

詳解IntelliJ IDEA 自帶的 HTTP Client 接口調用插件吊打 Postman

通過 cURL 創(chuàng)建請求,點擊右上角的 Convert form cURL 按鈕,然后輸入 cURL 地址即可自動轉換,如下:

詳解IntelliJ IDEA 自帶的 HTTP Client 接口調用插件吊打 Postman詳解IntelliJ IDEA 自帶的 HTTP Client 接口調用插件吊打 Postman

2.4 請求方式

GET

### GET request with a headerGET https://httpbin.org/ipAccept: application/json### GET request with parameterGET https://httpbin.org/get?show_env=1Accept: application/json### GET request with environment variablesGET {{host}}/get?show_env={{show_env}}Accept: application/json### GET request with disabled redirects# @no-redirectGET http://httpbin.org/status/301### GET request with dynamic variablesGET http://httpbin.org/anything?id={{$uuid}}&ts={{$timestamp}}###

POST

### Send POST request with json bodyPOST https://httpbin.org/postContent-Type: application/json{ 'id': 999, 'value': 'content'}### Send POST request with body as parametersPOST https://httpbin.org/postContent-Type: application/x-www-form-urlencodedid=999&value=content### Send a form with the text and file fieldsPOST https://httpbin.org/postContent-Type: multipart/form-data; boundary=WebAppBoundary--WebAppBoundaryContent-Disposition: form-data; name='element-name'Content-Type: text/plainName--WebAppBoundaryContent-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 bodyPOST https://httpbin.org/postContent-Type: application/json{ 'id': {{$uuid}}, 'price': {{$randomInt}}, 'ts': {{$timestamp}}, 'value': 'content'}###

PUT

PUT http://localhost:8080/person/putContent-Type: application/json{'name': '陳皮','age': 17}

PATCH

###PATCH http://localhost:8080/person/putContent-Type: application/json{'name': '陳皮','age': 17}

鑒權方式

### Basic authorization.GET https://httpbin.org/basic-auth/user/passwdAuthorization: Basic user passwd### Basic authorization with variables.GET https://httpbin.org/basic-auth/user/passwdAuthorization: Basic {{username}} {{password}}### Digest authorization.GET https://httpbin.org/digest-auth/realm/user/passwdAuthorization: Digest user passwd### Digest authorization with variables.GET https://httpbin.org/digest-auth/realm/user/passwdAuthorization: Digest {{username}} {{password}}### Authorization by token, part 1. Retrieve and save token.POST https://httpbin.org/postContent-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/headersAuthorization: Bearer {{auth_token}}###

斷言方式

### Successful test: check response status is 200GET 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 200GET 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-typeGET 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 bodyGET 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 接口調用插件吊打 Postman的詳細內容,更多關于idea HTTP Client插件的資料請關注好吧啦網其它相關文章!

標簽: IDEA
相關文章:
主站蜘蛛池模板: 泾川县| 阿城市| 沙河市| 沙雅县| 德庆县| 台安县| 平舆县| 郑州市| 永泰县| 霞浦县| 桑植县| 永丰县| 清河县| 普宁市| 舒城县| 成都市| 石台县| 潼南县| 伊金霍洛旗| 连云港市| 沅陵县| 通道| 虹口区| 道真| 漠河县| 浠水县| 商南县| 台中县| 阜阳市| 桓台县| 上杭县| 咸丰县| 兖州市| 岐山县| 定边县| 贞丰县| 株洲县| 灌云县| 井陉县| 长武县| 东方市|