新鈦云服已累計為您分享706篇技術(shù)干貨
一、 簡介
HTTP Client 是 IDEA 自帶的一款簡潔輕量級的接口調(diào)用插件,通過它,我們能在 IDEA 上開發(fā),調(diào)試,測試 RESTful Web 服務(wù)。
二、 快速上手
1、首先要確保 HTTP Client 插件是安裝啟動的,默認(rèn)是已安裝啟動的。若沒有安裝,在 File - Settings - Plugins 路徑下進(jìn)行安裝:
? ? ? ? ??? ? ? ? ??
2、可以在項(xiàng)目根目錄下創(chuàng)建一個存儲請求文件的文件夾,然后在里面創(chuàng)建HTTP Client 請求文件:
3、打開創(chuàng)建的文件,可以直接的點(diǎn)擊右上角工具欄中的add request, 選擇相應(yīng)的請求類型即可添加,如下圖所示get請求:?
? ? ?
4、點(diǎn)擊左邊的運(yùn)行按鈕即可發(fā)送請求,結(jié)果如下:
?
三、GET相關(guān)請求示例
###?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相關(guān)請求示例
###?Send?POST?request?with?json?body
POST?https://httpbin.org/post
Content-Type:application/json
{
??"id":?999,
??"value":?"content"
}
###?Send?POST?request?with?body?asparameters
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相關(guān)請求示例
PUT?http://localhost:8080/person/put
Content-Type:application/json
{"name":?"name111","age":?17}
六、PATCH相關(guān)請求示例
###
PATCH?http://localhost:8080/person/put
Content-Type:application/json
{"name":?"demo111","age":?17}
七、帶鑒權(quán)驗(yàn)證的示例
###
PATCH?http://localhost:8080/person/put
Content-Type:?application/json
{"name":?"demo111","age":?17}
七、帶鑒權(quán)驗(yà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");
});
%}
###
? ? 推薦閱讀 ??
文章來源:http://www.zghlxwxcb.cn/news/detail-779292.html
? ? 推薦視頻? ??文章來源地址http://www.zghlxwxcb.cn/news/detail-779292.html
到了這里,關(guān)于IDEA中的輕量級接口請求工具 | HTTP Client 新手指南的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!