IDEA HTTP Client (understand in seconds)


Recommendation: The strongest development environment series on the surface

A worker must first sharpen his tools if he wants to do well
The strongest development environment on the surface: vagrant+java+springcloud+redis+zookeeper mirror download (& detailed production)
The strongest hot deployment on the surface: java SpringBoot SpringCloud hot deployment hot loading hot debugging
The strongest requesting tool on the surface (goodbye, PostMan): IDEA HTTP Client (the most complete in history)
The strongest PPT gadget on the surface: Diao Zhentian, write PPT like code
No programming, no maker, no programming, no maker, a large wave of programming masters are communicating and learning in the crazy maker circle! Find an organization, GO

Recommendation: springCloud microservice series

Recommended reading
nacos actual combat (the most complete in history)
sentinel (the most complete + introductory tutorial in history)
springcloud + webflux high concurrency combat
Webflux (the most complete in history)
SpringCloud gateway (the most complete in history)
No programming, no maker, no programming, no maker, a large wave of programming masters are communicating and learning in the crazy maker circle! Find an organization, GO

Advantages of IDEA HTTP Client

IDEA HTTP Client debugging tool is really easy to use

(1) Scripted mock

(2) Copy-free token

… A lot

advantage

IDEA HTTP Client debugging tool is really easy to use

(1) Scripted mock

(2) Copy-free token

… A lot

PK with Postman

The previous scene with Postman was like this:

img

In fact, it is far more than these folders to classify the interfaces of multiple product lines. Postman is very powerful, but when faced with the following situations, I feel that debugging an interface is too troublesome (I will not discuss the quality of the tool here. Tools help us to improve efficiency, and everyone’s needs are different. I will only explain some of the situations I personally encounter. If you don’t like it, please don’t spray)

  • Most of the search configuration needs to be clicked with the mouse, which is contrary to the convenient way of accustomed to text and shortcut key operations
  • It is troublesome to import some of their data to debug other people’s interfaces
  • Viewing environment variables of multiple product lines is not intuitive
  • After writing the interface, switch the application back and forth for testing, such as (IDEA <——> Postman)
  • Quick positioning interface is more troublesome

IntelliJ IDEA of HTTP Clienttools just to solve some of the problems mentioned above,

Http Client Inspection Tour

The official demo animation:

img

For the enterprise version, HTTP Client is IDEA's default installation and enabled plug-in , as follows:

It looks like: The baby is in our computer, but I didn't find it!

Insert picture description here

If you don’t understand, let’s check it first:

Click on the menu: ToolsHTTP ClientTest RESTful Web Service

Insert picture description here

Next enter the following interface:

Insert picture description here

Fill in ip, port, path, click execute, you can see the result

Insert picture description here

Does it solve the problem of frequently switching between idea and postman , and postman can be thrown away.

Then, this is just the beginning of the use of the Http Client component! ! ! ! ! !

!!!!!!

Detailed description of the basis of the above figure:

Insert picture description here

Rest interface executable file

Did you see the title? It’s not wrong, just use a text file with a .http suffix (or .rest suffix) to manage the executable Rest interface and parameters uniformly to form a Rest interface executable file

The awesomeness of the Http Client component is this: Rest interface executable file

Method 1: Create by tool button

Insert picture description here

The default creates a named rest-api.httpfile, the file is stored in the Scratches file folder under the user directory.

Next, you can rest-api.httpexecute the Rest interface in the modified file, and the effect is as follows:

Insert picture description here

Method 2: Create HTTP request file directly

  • The simplest way to create in history:

Operate File—New—HTTP Request through the menu

Insert picture description here

The files are stored in the directory we specify, just like we usually create class/package.

  • The most direct way of creation in history:

Create a normal file with extension .http or .rest directly, over

Insert picture description here

The biggest advantage of HTTP request file:

It can be used as a project file and submitted to the warehouse through Git. Everyone shares files and maintains the interface request data together. Naturally, there will be no need to debug other people’s interfaces and import other people’s data.

In short, it is to facilitate the sharing of test cases, test parameters, and test data. Do you understand?

Interface syntax of Rest interface executable file

The syntax of the request is basically the same as the content of the http protocol, and the structure is as follows

### 使用三个 # 来分隔多个请求

GET/POST 请求地址(可拼接查询参数)
请求头键值对

请求体

> {% %}

# 响应处理
# 使用 >符号 打头,和 shell 很像,然后用 {% %} 括起来的脚本内容
# 在脚本中可以使用 javascript 原生语法,这就很强大了
# 脚本中有几个内置对象 client 表示当前客户端,response 表示响应结果

send request:

For some simple syntax in the .http file:

  1. Note: //or#
  2. Use three # signs to separate the request request: ###
  3. Basic or digest authentication for the web

eg: ordinary request body

POST http://example.com:8080/api/html/post
Content-Type: application/json 

{ "key" : "value", "list": [1, 2, 3] }

eg: do not add execution log

// @no-log
GET example.com/api

eg: web basic or digest authentication

// Basic authentication
GET http://example.com
Authorization: Basic username password

###

// Digest authentication
GET http://example.com
Authorization: Digest username password

Processing response:

Insert the script into the request

To >begin with, and then put the script in the package {%%}inside.

Assert client.assert

GET https://example.com/status/200

> {%
        client.test("Request executed successfully", function() {
        client.assert(response.status === 200, "Response status is not 200");
    });
%}

Script log client.log

client.log("this is log");

Insert picture description here

Storage variable

By client.global.setstoring global variables, by client.global.getacquiring variables. By clientthe object data stored in the memory, it may remain closed prior idea.

In addition to get values, you can also get values ​​through variables, as shown in the figure:

Insert picture description here

Then the variables are stored in one place, and they can be called in other places, which is very suitable for processing tokens. It is a bit like a browser login...

Accessibility description

RestfulToolkit

RestfulToolkit is also a plug-in, just search and install it in the plug-in market

Insert picture description here

After installing this plug-in, open the sidebar, all the interface information of the project will be displayed here:

Insert picture description here

My commonly used function is to copy the JSON data generated by the specified interface to the HTTP request file, which saves the trouble of handwriting. Is it convenient?

Live Template

The request content in the project is different. The GET POST request case provided by the IDEA standard may not meet our needs. At this time, we can use the Live Template to customize our own template and quickly generate the request content, like this:

img

JSON Viewer

JSON Viewer is a Chrome browser plug-in, enter json-viewer+ in the browser Omini-box Tab, paste json here, you can format the json data

img

Open the developer tools, double-click an HTTP request under Network, and the returned json data will be automatically formatted under the new tab, eliminating the trouble of pasting and formatting the data

Common examples

Configure environment information

This is also its relatively strong hand, use a json to configure all the environment, as long as you can read about at the request of the scripts directory, follow the agreed principle larger than the configured, the file name http-client.env.jsonrepresents the common variables can put local http-client.private.env.jsonrepresentation passwords and other private variable configuration The configuration is roughly as follows. When running the request script, idea will prompt you which environment to choose

{
    
    
  "local":{
    
    
    "baseUrl": "http://localhost:8083/",
    "checkCode": "http://localhost:8081/sanritools/company/token?env=dev",
  },
  "dev": {
    
    
    "baseUrl": "http://devhost:port/",
    "checkCode": "http://localhost:8081/sanritools/company/token?env=dev",
  },
  "sit": {
    
    
    "baseUrl": "http://sithost:port/",
    "checkCode": "http://localhost:8081/sanritools/company/token?env=sit",
  }
}

The above files are placed in the same directory as the rest interface files.

The above file defines three environment types: sit, local, dev. When executing, you need to select the type of environment:

Insert picture description here

Login token example

The examples on the Internet basically say that the token is obtained using an interface, and then saved in a global variable, and then the subsequent interface can be used directly. The code is as follows, but I tried countless times without success. Furthermore, Does your project have no verification code?

### 获取 token 值
GET {
    
    {
    
    checkCode}}
Content-Type: application/json

> {
    
    %
client.global.set("token", response.body.token);
%}

### 获取素材列表
POST {
    
    {
    
    baseUrl}}/media/list
Content-Type: application/json
Authorization: {
    
    {
    
    token}}

{
    
    
  "pageNo": 1,
  "pageSize": 10
}

Above this is a problem, the return value response.body other is not javascript json string objects, you need to resolve what, in front said, and here you can use the native javascript script, so JSON.parse(response.body).tokenin order to get the correct token,

The second question, our token now exists in redis, so I can bypass the verification code and write another interface to redis to get the token directly. The final script is as follows

### 获取 token 值
GET {
    
    {
    
    checkCode}}
Content-Type: application/json

> {
    
    %
client.global.set("token", JSON.parse(response.body).token);
%}

### 获取素材列表
POST {
    
    {
    
    baseUrl}}/media/list
Content-Type: application/json
Authorization: {
    
    {
    
    token}}

{
    
    
  "pageNo": 1,
  "pageSize": 10
}

File transfer practices

Said earlier, input and output and the shell like, so use this to enter a file < 文件地址, just find multipart/form-datathe data format look, I know how to pass the document, in fact, there are examples, as shown in the following script, which uses a The key of the boundary is bound to the data of multiple parts currently to be submitted, and each part can be in a different data format;

### 测试发送文件信息(文件元数据信息查询)
POST {
   
   {baseUrl}}/media/upload/fileMetaData
Content-Type: multipart/form-data; boundary=WebAppBoundary
Authorization: {
   
   {token}}

--WebAppBoundary
Content-Disposition: form-data; name="fileSize"
Content-Type: application/json

1024
--WebAppBoundary
Content-Disposition: form-data; name="md5"
Content-Type: application/json

sdafasdfasfd
--WebAppBoundary
Content-Disposition: form-data; name="partFile"; filename="zhongwenbuxin.avi"
Content-Type: application/octet-stream

< C:\Users\091795960\Videos/V0106_1024KB.avi
--WebAppBoundary--

I did not encounter the garbled code of the returned message, nor did I set the message header and startup configuration mentioned on the Internet. The returned message was coded normally; I only encountered that the uploaded file name was garbled, and the setting did not take effect. No solution was found. The solution, but for self-test, it doesn’t matter if you choose an English name. Anyone who solves the problem can tell me.

Regarding the use of other assertions and the use of logs, you can find other blogs, here is an article for reference

Guess you like

Origin blog.csdn.net/crazymakercircle/article/details/113201185