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 great 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.

There are several reasons for the law of true fragrance:

1. First of all, Postman has all the functions of IDEA REST Client, such as REST Client console and history request record

Second, why switch to a production tool when you can do development and debugging in one

3. 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 method, request parameters, and request header padding.

In particular, if the Authorization :Basic authentication is requested, you can click the button as shown in the following figure, and a window will pop up to fill in the user name and password, which will be automatically added to the Authorization header after completion

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. A few awesome IDEA plug-in, recommend everyone to see.

In addition, you can follow the wechat public account “Java Technology stack”, in the background reply: IDEA, you can get the latest N IDEA tutorials I organized, are dry goods.

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., An environment object is an environment variable that can be retrieved from an HTTP request. You can retrieve this parameter directly from 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 temporarily stored to the environment variable

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.

This article has been updated on the Java Technology Stack website:

1.Java JVM, Collections, Multithreading, new features series tutorials

2.Spring MVC, Spring Boot, Spring Cloud series tutorials

3.Maven, Git, Eclipse, Intellij IDEA series tools tutorial

4.Java, backend, architecture, Alibaba and other big factory latest interview questions

Life is good. See you tomorrow