New skills to quickly test API interfaces

Original address: Liang Guizhao's blog

Blog address: blog.720ui.com

In the daily development process, we are more or less involved in the testing of API interfaces. For example, some friends use Chrome's Postman plug-in, or use tools such as Firefox's restclient. In fact, these tools are one of the most effective ways to test API interfaces, and I have been using Postman to complete API interface testing before. Today, the author recommends another very useful gadget that can help readers quickly test API interfaces. This tool is IDEA's Editor REST Client.

IDEA's Editor REST Client has been supported since IntelliJ IDEA 2017.3 version, and many features have been added in 2018.1 version. In fact, it is an HTTP Client plugin for IntelliJ IDEA .

start working

First, we can create a xxx.http file in any directory. ,as the picture shows.

Here, three ### perform HTTP request splitting. In fact, a file can contain multiple HTTP requests, and comments can be added after ### , as shown in the example below.

### 用户登录
POST http://localhost:8088/oauth/token?grant_type=password&username=lgz&password=123456
Accept : application/json
Content-Type : application/json;charset=UTF-8
Authorization: Basic client secret
Cache-Control : no-cache

So we get the response content.

Multi-environment configuration

In the development process, we usually have multiple sets of environments, such as development environment , * testing environment , pre- production environment , production environment , etc. So it would be great if Editor REST Client could do multi-environment configuration like Postman. In fact, Editor REST Client already supports this feature, we just need to create the rest-client.env.json file and configure the multi-environment information.

{
  "development" : {
    "url" : "http://localhost:8088",
    "token" : "Bearer 4d74c7fb-0ef4-45ec-b3ff-902eaa3d116c"
  },
  "test" : {
    "url" : "http://localhost:8089",
    "token" : "Bearer 4d74c7fb-0ef4-45ec-b3ff-902eaa3d116c"
  },
  "preproduction" : {
    "url" : "http://activity.720ui.com",
    "token" : "Bearer 4d74c7fb-0ef4-45ec-b3ff-902eaa3d116c"
  },
  "product" : {
    "url" : "http://activity.720ui.com",
    "token" : "Bearer 4d74c7fb-0ef4-45ec-b3ff-902eaa3d116c"
  }
}

At this point, the previous URL is transformed, and http://localhost:8088 is transformed into {{url}} instead.

POST {{url}}/oauth/token?grant_type=password&username=lgz&password=123456

Here, we obtain the overall effect.

Detailed case explanation

Now, let's write a complete case. For specific configuration, please refer to: REST Client

### 创建用户信息
POST {{url}}/v1/m/users
Accept : application/json
Content-Type : application/json;charset=UTF-8
Authorization: {{token}}
{
    "username": "xiaoyue",
    "realname": "小岳",
    "password": "111111",
    "email": "[email protected]",
    "tel": "18305930000",
    "weixin": "LiangGzone",
    "sex": 1
}

### 修改用户信息
PUT {{url}}/v1/m/users/723181
Accept : application/json
Content-Type : application/json;charset=UTF-8
Authorization: {{token}}
CachePATCH-Control : no-cache
{
    "username": "xiaoyue"
}

### 查询用户信息
GET {{url}}/v1/c/users/lgz/username
Accept : application/json
Content-Type : application/json;charset=UTF-8
Authorization: {{token}}

### 查询用户信息列表
GET {{url}}/v1/c/users?keyword=梁
Accept : application/json
Content-Type : application/json;charset=UTF-8
Authorization: {{token}}

References

(End, reprint please indicate the author and source.)

More exciting articles can be found on the WeChat public account of "Server Thinking"!

image

I invite you to join my planet, a group of people with the same frequency, grow together, and look back at the past one year later.

image

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325382297&siteId=291194637