IDEA中的HTTP Client Editor测试API
生活随笔
收集整理的這篇文章主要介紹了
IDEA中的HTTP Client Editor测试API
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
在前后端分離項目,前后端通過api進行通信。如果用postman免費版進行api測試的話,由于無法保存測試腳本到文件,不方便前端查看。
你可以選擇付費版。也可以利用IDEA自帶的HTTP Client Editor編寫測試腳本。這里寫個demo主要是方便查詢語法。
HTTP請求
### 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###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-urlencodedid=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/plainName --WebAppBoundary Content-Disposition: form-data; name="data"; filename="data.json" Content-Type: application/json< ./request-form-data.json --WebAppBoundary--###認證請求
### 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"); }); %}###?
上面這些腳本是很好的demo,涵蓋了語法,并且可以直接點擊運行。
轉載于:https://my.oschina.net/wuweixiang/blog/1862372
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的IDEA中的HTTP Client Editor测试API的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一文读懂生成对抗网络(GANs)
- 下一篇: laravel 错误与日志