Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of IDEA REST Client

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shares with you the content of the sample analysis of IDEA REST Client. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Preface

Interface debugging is an indispensable skill for every software development practitioner. The completion of a project may take more time for interface testing and debugging than for real development and writing code, and it is almost a daily work item for every development. As the saying goes, you must sharpen your tools if you want to do good work. Before you taste the true smell of IDEA REST, postman (a plug-in of chrome) is indeed a very good choice, with complete REST Client function and request history function. But after using IDEA REST, postman can be lost, because IDEA REST Client has all the functions of postman, and there are functions that postman does not have, so move on.

From postman to IDEA REST Client

The reasons for the law of true incense are as follows:

First of all, IDEA REST Client has all the functions of postman, such as the REST Client console and historical request records.

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

IDEA REST Client then supports environment configuration differentiation, as well as the ability of interface response assertions and scripted processing

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

IDEA REST Client console

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

As you can see, the function area displayed in this console is no different from postman, including the request method, the filling of request parameters and request headers. In particular, if the request method is authenticated by Authorization: Basic, you can click the button shown below, and a window filled with user name and password will pop up. After filling it, it will be automatically added to the header of Authorization.

History request record

IntelliJ IDEA automatically saves 50 recently executed requests to a 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 a request again. The content of the file is as large as shown in the following figure. Just click the run button to issue the request again. If the 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 HTTP request script

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

Grammar part

# demonstrate POST request POST {{baseUrl}} get?show_env=1Accept: application/json {"name": "a"} # demonstrate GET request GET {{baseUrl} / postContent-Type: application/x-www-form-urlencodedid=999&value=content

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

Environmental distinction

Careful you may find the above example code, there is no real request address, instead, is a {{baseUrl}} placeholder, this is the true fragrance of IDEA REST Client, support to get environment-related configuration parameters from the specified configuration file, not only baseUrl can be replaced by placeholders, some requested parameters if related to the interface environment can be distinguished through the configuration file.

First, create a file named http-client.private.env.json in the same directory as the .http script, and then the contents are as follows. The key values at the first level are 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 a HTTP request. You can directly obtain the parameters configured here through the {{xx}} placeholder 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 the configuration of that environment, such as:

Result assertion

IDEA REST Client can script assertions against the response value of an interface, immediately rising from an interface debugging tool to a test 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 this scenario: when a system needs to be authenticated before it can be accessed, if you use postman, do you first access the login interface and then manually paste and copy it into the header parameters of the new debug interface after obtaining token? this is too troublesome. IDEA REST Client also has a really fragrant feature that can solve this problem perfectly. Please see the following script:

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

After the first authentication request is completed, the returned token information can be obtained in response, and then we set it to the global variable through script, so in the following interface request, we can directly obtain the token by using double braces placeholder.

Thank you for reading! This is the end of this article on "sample Analysis of IDEA REST Client". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report