• You have one thought, I have one thought, and when we exchange, one person has two thoughts

  • If you can NOT explain it simply, you do NOT understand it well enough

Now the Demo code and technical articles are organized together Github practice selection, convenient for everyone to read and view, feel good, please also Star🌟

Working from home. Work as usual, the leader needs several new functional interface development. Used to work in the company, usually developed functions no problem, exposed Swagger interface document, directly find next to the front-end adult joint test

At present, you are at the head of the Yangtze River, I am at the end of the Yangtze River. Every night I think you dare not go out to see you. Ah, all the tests are up to you.

Although I miss the front end, working from home is definitely just as productive as working in a corporate office

Call waiting background

Switching between development functions across multiple product lines, Postman used to look like this:

Actually much more than this a few folders to classify the interface of multiple product lines, Postman function is very powerful, but in the face of these situations, I think debugging an interface is too much trouble, don’t discuss here tools, tool is to help us improve efficiency, each person’s demand is different also, I just shows that I met some of the personal situation, Don’t spray if you don’t like it)

  • Finding configurations is mostly a point-and-click process, contrary to the convenience of text and keyboard shortcuts
  • Debugging others interface to import some of their data, more trouble
  • Multiple product line environment variables are not intuitive to view
  • After writing the interface, switch the application back and forth for testing, such as (IDEA < — > Postman).
  • It is difficult to quickly locate interfaces
  • .

IntelliJ IDEA’s HTTP Client solves some of the problems I mentioned above, simply creating, editing, and executing HTTP requests directly in the IDEA code editor, like this (if you’re interested, read on) :

So, go to the official website to check and make the following arrangement:

Into the Http Client

HTTP Client is a plugin for IDEA that is bound and enabled by default. If you do not have it enabled, follow the following figure to enable it

Click Tools – HTTP Client – Test RESTful Web Service

Enter the following interface:

As indicated above, REST Client is deprecated. Click Convert Request to New Format on the right to enter the following interface:

A file named rest-api.http will be created by default and stored in the Scratches folder. To highlight the Scratches theme, look for Scratch Files on the Scratches website. The yellow box is also useful

Create an HTTP request file

HTTP is an HTTP request file that can be created in two ways:

  1. By shortcut keys⇧ ⌘ NThen selectHTTP Request. (Files are stored in the Scratches folder)
  2. File — New — HTTP Request (File in our specified directory, just like we would create class/package)

If it is used in a project, the second method is recommended, because it can be submitted to the repository through Git as a project file, so that everyone can share the file and maintain the interface request data together. Naturally, there will be no need to debug other people’s interface and import other people’s data

Edit the HTTP request file

We simulate a real project scenario to edit the file

  1. The user logs in and obtains the Token, usually in a POST request
  2. The Token returned by successful login must be carried in the request header for subsequent user access

You can write by clicking Add Request and selecting the appropriate method

As we all know, usually writing a complete request needs to write a lot of content, thoughtful IDEA provides us with templates, we just need to find templates in Examples, such as POST request template, select the appropriate copy can be, so easy ~ ~ ~ ~

At this point, you can send basic requests, but how can you quickly generate parameters in a project with so many interfaces? How to switch ports quickly? How do I make every request after login automatically carry a Token returned successfully? We need more advanced gameplay

HTTP Client advanced gameplay

Using environment variables

When writing an HTTP request, variables can be used to parameterize its elements. Variables can hold the request’s host, port, and path, query parameters or values, request header or request body values, and so on.

The way to use a variable is very simple, just surround the defined variable with two curly braces, like this:

Of course, we also need a place to define variables

Defining environment variables

Environment variables need to be defined in environment files (in the same directory as request files). There are two types of environment files:

  1. Create arest-client.env.jsonorhttp-client.env.jsonThis is the environment file that holds the JSON data, which defines all the general variables that can be used throughout the project
  2. Create arest-client.private.env.jsonorhttp-client.private.env.jsonBy default, this file is added to the IGNORE file of the VCS and has a higher priority than other environment files. In other words, variables in this file will override variables in other environment files

The contents of the file look something like this

{
  "dev": {
    "host": "localhost"."port": 8081."identifier": "tanrgyb"."password": "iloveu"
  },
  "prod": {
    "host": "dayarch.top"."port": 8080."identifier": "admin"."password": "admin"}}Copy the code

Run the request we wrote:

IDEA automatically identifies multiple environments, so you can easily switch between environments and use different variable values (the shoes are brighter or not)

Clever use of response Handler scripts

As mentioned above, we want all requests after a successful login to automatically carry the Token returned successfully, so instead of manually adding it to the header each time, there are also two ways to insert the script into the request

  • Embedded way
GET host/ API /test > {% response script %}Copy the code
  • External file mode (that is, pull the embedded script out of the file)
GET host/api/test

> scripts/my-script.js
Copy the code

Take login to return the obtained token set into a variable as an example, look at the code:

POST http://{{host}}:{{port}}/login
Content-Type: application/json
Accept: application/json

> {%
client.global.set("auth_token", response.body.result.token);
 %}
Copy the code

Pay attention to

Response. The body. The result. The token is I wrote as I log in the returned data structure, different structure, you can be such a response. The body. The token of the response. After the body according to your data structures play

I still don’t trust, put my login back structure (how to design this structure in the project, can refer to the previous written Springboot return uniform JSON data format is how to achieve?) Let me paste it here. Does that make sense?

Then we can happily carry this Token on other requests

Pay attention to

The Authorization types here have been modified according to their actual conditions, such as: Authorization: Bearer {{auth_token}}

The above has satisfied my daily use, and I don’t know more about it. For more usage of Response script, please check HTTP Response Reference on the official website

You thought that was the end of it (OMG), and there are spices to share, which is great to use with the above feature

Auxiliary Function Description

RestfulToolkit

RestfulToolkit is also a plug-in and can be installed by searching in the plug-in market

After installing the plug-in, open the sidebar and all the interface information for the project is displayed here:

My common function is to copy the JSON data generated by the specified interface to the HTTP request file, without the trouble of handwriting, do you think it is convenient?

In addition, using the shortcut key CMD +\, you can quickly find the interface according to the keyword, enter the code interface quickly to the location, which also brings great convenience

Live Template

The request content varies from project to project, and the GET and POST request cases provided by IDEA standard may not meet our requirements. In this case, we can use the Live Template to customize our own Template and quickly generate the request content, like this:

JSON Viewer

JSON Viewer is a Chrome plugin that formats JSON data by typing JSON-viewer + Tab in omini-box

Open the developer tool, double-click an HTTP request under the Network, and the returned JSON data will be automatically formatted under the New TAB, eliminating the trouble of pasting data and then formatting

About self test interface dry goods I shake almost, shake more healthy

conclusion

Again, not to be a tool party, not to be critical in any way, tools are just to let us work more efficiently, choose what works for us. From the introduction above, IDEA HTTP Client with several auxiliary functions I said a good solution to the beginning of the article explained several problems, for my personal situation, enough!!

Screenshot code words are not easy

  • Please also “read/retweet/like” if you found this article helpful (this is my big rocket πŸš€, big sports car πŸš—, big plane ✈️)

  • If you find a better function, please add it in the comment area, I will continue to add this content, thank you very much

  • The official number replied “tool”, and there are more wonderful waiting for you

Soul asking

  1. Kibana Dev Tools is used to debug the ES interface, which is similar to this interface
  2. How did you effectively test interfaces and tuning on your project? Please be generous with your advice
  3. How has working from home and in the office affected you?

reference

  • Testing RESTful web services

Personal blog: https://dayarch.top

Add my wechat friends, enter the group entertainment learning exchange, note “enter the group”

Welcome to continue to pay attention to the public account: “One soldier of The Japanese Arch”

  • Cutting-edge Java technology dry goods sharing
  • Efficient tool summary | back “tool”
  • Interview question analysis and solution
  • Technical data to receive reply | “data”

To read detective novel thinking easy fun learning Java technology stack related knowledge, in line with the simplification of complex problems, abstract problems concrete and graphical principles of gradual decomposition of technical problems, technology continues to update, please continue to pay attention to……