Idea使用http工具

創建以.http結尾的文件,用構建http請求

GET方式(傳參用?在url後拼接即可)

GET http://localhost:8080

POST方式(參數放到請求體中,需要在url下空一行後寫到json中即可)

POST http://localhost:8080
Content-Type: application/json

{"username":"test","password":"123456"}

往headers中添加屬性(在url下直接寫即可)

POST http://localhost:8080
Content-Type: application/json
token:test

創建以.env.json公共的環境文件,用以存儲公共信息

創建以.private.env.json私有的環境文件,用以存儲隱私信息

兩種內容結構相同,在調用rest接口時,選擇環境即可

{
  "dev": {
    "api": "http://localhost:8080",
    "token": "test"
  },
  "prod": {
    "api": "http://localhost:8080",
    "token": "test"
  }
}
### 在請求中就可以這麼寫
### GET示例
GET {{api}}/test
token:{{token}}

### POST示例
POST {{api}}/test
Content-Type: application/json
token:{{token}}

{
	"username":"test",
	"password":123456
}

在請求中使用腳本

### 登錄成功後將拿到的token數據設置到環境中的token變量,以便其他接口使用
### 例如:登錄成功後返回:{"result":{"token":"test"}}
POST {{api}}/login
Content-Type: application/json

{
	"username":"test",
	"password":123456
}
> {%
client.global.set("token", response.body.result.token);
 %}

### 在其他請求中則可直接使用
GET {{api}}/test
token:{{token}}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章