Use restfultool
test controller
@RestController
@RequestMapping("/api")
@Slf4j
public class ApiController {
@GetMapping("test1")
public String test1(@RequestParam String msg) {
return "hello world " + msg;
}
@GetMapping("test2/{msg}")
public String test2(@PathVariable String msg) {
return "hello world " + msg;
}
@PostMapping("test3")
public String test3(@RequestBody Body body) {
return "hello world " + body.name;
}
@Data
static class Body {
private String name;
private int age;
}
}
Request header setting method, base64 encryption
The user name is user, the password is 123456, pay attention to the semicolon in the middle
httpclient uses
environment variable
The concept of environment variables
According to official documents, the following two files can be created inside the project:
①rest-client.env.json (or http-client.env.json) is an ordinary file containing regular variables. (All regular variables used in the entire project can be defined in this file)
② Rest-client.private.env.json (or http-client.private.env.json) is a private file, which may include passwords, order licenses, certificates, and other sensitive information. By default, this file is added to VCS' list of ignore files. Values of variables specified in the http-client.private.env.json file will override values in the environment file. because it has the highest priority.
Create configuration file http-client.env.json
{
"dev": {
"user": "user",
"password": "123456",
"token": "dXNlcjoxMjM0NTY="
},
"prod": {
"user": "user",
"password": "123456",
"token": "123"
}
}
The specific use of environment variables
We define a dev and a prd in the environment variable. When running the interface, just choose which environment to run.
If there is a request header, you can use the first method that is all in plain text, or you can use the second method that encrypts the token with base64.
If it is a post request, the request body needs to add Content-Type : application/json. Note that the following body needs to be empty to avoid being regarded as part of the url.
Script detection, you can print the return value, assert and other operations.
dynamic variable
Dynamic variables generate a value each time you run a request:
$uuid: generate a universally unique identifier (UUID-v4)
$timestamp: generate the current UNIX timestamp
$randomInt: Generates a random integer between 0 and 1000.
例如:GET http://localhost:8080/api/test1?msg={ {$timestamp}}
###
GET http://localhost:8080/api/test1?msg=test1
#Authorization: Basic {
{user}} {
{password}}
Authorization: Basic {
{token}}
> {%
client.log(JSON.stringify(response.body));
client.test("Request executed successfully", function() {
client.assert(response.status === 200, "Response status is not 200");
});
%}
###
GET http://localhost:8080/api/test2/test2
Authorization: Basic {
{token}}
> {%
client.test("Headers option exists", function() {
client.assert(response.body.hasOwnProperty("headers"), "Cannot find 'headers' option in response");
});
%}
###
POST http://localhost:8080/api/test3
Authorization: Basic {
{token}}
Content-Type: application/json
{
"name":"test3",
"age":"18"
}
> {%
client.test("Request executed successfully", function() {
client.assert(response.status === 200, "Response status is not 200");
});
client.test("Response content-type is json", function() {
const type = response.contentType.mimeType;
client.assert(type === "application/json", "Expected 'application/json' but received '" + type + "'");
});
%}
管理 Cookies
Cookies存储在.idea/httpRequests/目录下的HTTP-client.cookies文件中。
最多存储300个Cookies。
@注释
@name
给API接口取名,否则IDEA默认以#1开始编排。
# @name apiName
@no-log
返回结果不记录在.idea/httpRequests/文件夹下,适用于一些不适合保存的敏感数据。
# @no-log
@no-cookie-jar
请求时不携带Cookies。
# @no-cookie-jar
@no-redirect
服务端返回301等重定向状态码时,不请求重定向后的地址,直接返回301的结果。
# @no-redirect
@use-os-credentials
# @use-os-credentials
转换成 cURL
方法一
将光标点击到需要转换的HTTP请求上,点击顶部Convert to cURL and Copy,即可将HTTP Client中声明的请求复制成cURL并且拷贝到粘贴板中。
方法二
将光标点击到需要转换的HTTP请求上,按下Alt+Enter快捷键,选择Convert to cURL and copy to clipboard,即可将HTTP Client中声明的请求复制成cURL并且拷贝到粘贴板中。
更多用法
HTTP Client还支持:GraphQL、WebSocket。
===:分割WebSocket的每条信息。
=== wait-for-server:等待WebSocket服务端返回信息。
参考
https://www.jetbrains.com/help/idea/exploring-http-syntax.html
视频讲解
https://www.bilibili.com/video/BV1Xa411978P