[IDE Tool] It’s time to throw away Postman, and another underestimated IDEA plugin comes out

# Preface

 

Interface debugging is an indispensable skill for every software development practitioner. For the completion of a project, the time for interface testing and debugging may be more than the time for actual development and writing of code. It is almost every day-to-day work item of development.

 

The so-called good work must first sharpen their tools, before you taste the true fragrance of IDEA REST, postman (a plugin for chrome) is indeed a very good choice, with a complete REST Client function and request history function. But when IDEA REST is used, postman can be lost, because IDEA REST Client has all the functions of postman, and there are functions that postman does not have, so continue to look down.

 

# From postman to IDEA REST Client

 

There are several reasons for the true incense law:

 

1. First of all, all functions of postman IDEA REST Client are available, such as REST Client console and historical request record

2. Secondly, if you can complete development and debugging in one production tool, why switch to another tool?

3. Then IDEA REST Client also supports the function of distinguishing environment configuration, as well as the ability of interface response assertion and scripting processing

4. The request configuration of IDEA REST Client can be described by file configuration, so it can be shared with the project and project members

 

# IDEA REST Client console

 

After opening Tools -> HTTP Client -> Test RESTFUL Web Service from the top toolbar, the interface of IDEA REST Client console is as follows:

 

As you can see, there is no difference between the functional area displayed in this console and postman, including the request method, the filling of request parameters and request headers. The special note is that if the request method is Authorization: Basic. For authentication by way of authentication, you can click the button shown in the figure below, and a window for filling in the user name and password will pop up, and it will be automatically added to the Authorization header after filling in

 

 

# History request record

 

IntelliJ IDEA automatically saves the most recently executed 50 requests to the http-requests-log.http file, which is stored in the .idea / httpRequests / directory of the project. Using request history, you can quickly navigate to a specific response and make the request again.

 

The content of the file is large as shown in the figure below. To make a request again, just click the run button. If a request is issued again from the request history, its execution information and a link to the response output will be added to the top of the request history file.

 

 

# Build HTTP request script

 

The above history record is a complete IDEA REST Client request script. If you trigger it from the console, you can directly copy the file of the history request record and put it in the project as the HTTP request script to share with other members. If not, You can also directly create a new file ending with .http or .rest, IDEA will automatically recognize it as an HTTP request script.

 

# Grammar part

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
### 演示POST请求POST {
   
   {baseUrl}}}get?show_env=1Accept: application/json
{
   
      "name":"a"}### 演示GET请求
GET {
   
   {baseUrl}}}/postContent-Type: application/x-www-form-urlencoded
id=999&value=content

First, separate each request body by ###three hash keys, and then the request url and header parameters are next to each other. Whether the request parameters are POST body parameters or GET parameter parameters, they must be wrapped. of

 

# Environmental distinction

 

Careful you may find that the code in the above example does not have a real request address. Instead, it is a placeholder for { {baseUrl}}. This is where IDEA REST Client is really fragrant. It can be obtained from the specified configuration file. Environment-related configuration parameters, not only baseUrl can be replaced by placeholders, but some requested parameters can be distinguished by configuration files if they are related to the interface environment.

 

First, create a file named http-client.private.env.json in the same directory of the .http script, and then the content is as follows, the first-level key value is used to distinguish the environment, such as dev, uat, pro, etc. The objects in the environment are the environment variables that can be obtained in an HTTP request. You can directly obtain the parameters configured here by way of { {xx}} placeholders in the requested HTTP script

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

 

Then when you choose to execute the request, IDEA will let you choose to execute the configuration of that environment, such as:

 

 

# Result assertion

 

IDEA REST Client can perform scripted assertion processing for the response value of the interface, immediately rising from an interface debugging tool to a testing tool.

such as:

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
### Successful test: check response status is 200GET https://httpbin.org/status/200
> {%client.test("Request executed successfully", function() {
   
     client.assert(response.status === 200, "Response status is not 200");});%}

 

# Result value is temporarily stored

 

Imagine a scenario like this. When a system needs to be authenticated to access it, if you use postman, do you first access the login interface, and then obtain the token, manually paste and copy it into the header parameter of the new debugging interface? Too much trouble, IDEA REST Client also has a really fragrant function, which can perfectly solve this problem. Please see the following script:

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
### 演示POST请求POST https://httpbin.org/postContent-Type: application/json
{
   
     "user": "admin",  "password": "123456"}
> {% client.global.set("auth_token", response.body.json.token); %}### 演示GET请求
GET https://httpbin.org/headersAuthorization: Bearer {
   
   {auth_token}}
 

After the first authentication request is over, you can get the returned token information in the response, and then we set it to the global variable through the script, then in the next interface request, you can directly use the double brace placeholder To get this token

 

# Conclusion

 

Postman has a reputation and is indeed a very good must-have tool. I always recommend this tool to Biren. However, IDEA REST Client is also really good. It is worth trying. Later, Amway will switch to IDEA REST Client, and I lost the postman anyway. When you interface with a third party, a rest-http.http interface request file is necessary in the project, which is convenient for others while satisfying yourself.

Guess you like

Origin blog.csdn.net/suifeng629/article/details/103207550