The author | kai-ling Chen

Source | my.oschina.net/keking/blog/3104972

Interface debugging is an essential skill for every software development practitioner, the completion of a project, interface testing and debugging time may be more than the real development and writing code time, almost every development of the daily work item.

Postman is a good choice before you taste IDEA REST, with full REST Client functionality and request history. However, postman can be discarded when using IDEA REST, because IDEA REST Client has all the features of Postman, but also features that Postman does not have.

From Postman to IDEA REST Client

The reasons for this rule are as follows: 1. Postman has all the functions of IDEA REST Client, such as REST Client console and history request record 2. Second, why switch to a production tool when you can do development and debugging in one. The IDEA REST Client also supports environment configuration differentiation and the ability of the interface to respond to assertions and scripting. 4. The IDEA REST Client request configuration can be described in a file configuration, so it can be shared with projects and project members

IDEA REST Client console

On the top toolbar, choose Tools > HTTP Client > Test RESTFUL Web Service. The interface of the IDEA REST Client console looks like the following:

As you can see, the console displays the same functionality as Postman, including the request mode, request parameters and request header padding. In particular, if the request mode is Authorization :Basic, you can click the button shown in the figure below. A window will pop up to fill in the user name and password, which will be automatically added to the Authorization header

History request record

IntelliJ IDEA automatically saves the last 50 requests to the http-requests-log. HTTP file, which is stored in the project’s. IDEA/httpRequests/directory. Using request history, you can quickly navigate to a particular response and request again.

The size of the file is as shown in the image below. Just click the run button to make the request again. If a request is made again from the request history, a link to its execution information and response output is added to the top of the request history file.

Build the HTTP request script

This is a complete IDEA REST Client request script. If you trigger it from the console, you can copy the history request file into the project as an HTTP request script and share it with other members. If not, You can also create a. HTTP or. Rest file. IDEA automatically identifies it as an HTTP request script.

Grammar part

POST {{baseUrl}} get? Json {"name":"a"} ### application/x-www-form-urlencoded id=999&value=contentCopy the code

Each request body is separated by ### three hash keys, and the url and header parameters are next to each other. The request parameters, whether the body parameter of POST or the parameter parameter of GET, are wrapped

Environment to distinguish the

If you are careful, you may have noticed that the code in the above example does not have a real request address. Instead, it has a placeholder {{baseUrl}}. This is where the IDEA REST Client is really interesting. Not only can baseUrl be replaced by placeholders, but some request parameters that are relevant to the interface environment can be distinguished by configuration files.

At first. With HTTP script to create a directory named HTTP – client. Private. The env. Json file, and then the content is as follows, the primary key value used to distinguish the environment, such as, dev, uat, pro, etc., Context objects are environment variables that can be retrieved in an HTTP request. You can retrieve these parameters directly in the HTTP script via {{xx}} placeholders

{
  "uat": {
    "baseUrl": "http://gateway.xxx.cn/",
    "username": "",
    "password": ""
  },
  "dev": {
    "baseUrl": "http://localhsot:8888/",
    "username": "",
    "password": ""
  }
}Copy the code

IDEA allows you to select the configuration of the environment to execute the request, such as:

Results the assertion

The IDEA REST Client can perform scripted assertion processing on the response value of the interface, which is immediately upgraded from an interface debugging tool to a testing tool, such as:

### 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");
});
%}Copy the code

The result value is temporary

If you use postman, you need to access the login interface first. After obtaining the token, you need to manually paste and copy it into the header parameter of the new debugging interface. This is too troublesome. The IDEA REST Client also has a nice feature that solves this problem perfectly, as shown in the following script:

### demo POST request POST https://httpbin.org/post content-type: application/json {"user": "admin", "password": "123456" } > {% client.global.set("auth_token", response.body.json.token); %} # # # demo GET request GET https://httpbin.org/headers Authorization: Bearer {{auth_token}}Copy the code

At the end of the first authenticated request, we can get the token information returned in response, and then we set it in the global variable through the script. Then in the following interface request, we can directly obtain the token in the form of double curly bracket placeholder.

conclusion

Postman is well known and is indeed a very good must-have tool. Before I recommended this tool to others, I always recommended postman to others. However, IDEA REST Client is really good and worth a try. Later, the amway tool was switched to IDEA REST Client, but I lost postman anyway. When interconnecting with a third party, a REST-HTTP interface request file is required in the project to satisfy the needs of others.

If this article is helpful to you, please remember to like follow yo ~

Welcome to pay attention to my public number: love IT, daily push technical articles for everyone to learn reference.