如何高效使用IDEA的HTTP Client进行网络请求开发?

2026-04-13 05:182阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1205个文字,预计阅读时间需要5分钟。

如何高效使用IDEA的HTTP Client进行网络请求开发?

介绍IDEA RESTful WebServices是一个类似JMeter、Postman的工具。可以使用纯文本编辑器。官方介绍地址:https://www.jetbrains.com/help/idea/restful-webservices.。该工具是IDEA的一个组件,在‘Tools-Http Client’中。

介绍

IDEA RESTful WebServices是一个类似jmeter,postman的工具。可以使用纯文本编辑。

官网介绍地址:www.jetbrains.com/help/idea/restful-webservices.html

该工具是idea的一个组件,在Tools->Http client下;当然goland也是相同;低版本是Test Restful WebService,新版本的idea已经提示改功能废弃,建议使用new HTTP Client也就是我们此教程要介绍的工具;

示例:

创建demo1.www.baidu.com

###

点击右侧运行即可查看到结果

HTTP请求中使用变量

要在请求中提供变量,请将其括在双花括号中,如 {{variable}} 。变量名称只能包含字母,数字,下 划线符号 _ 或连字符 - 。

预定义的动态变量

每次您运行请求时,动态变量都会生成一个值: $uuid :生成通用的唯一标识符(UUID-v4) $timestamp :生成当前的UNIX时间戳 $randomInt :生成介于0到1000之间的随机整数。

GET localhost/api/get?id={{$uuid}}

创建环境变量

在项目内部,创建以下文件:

  • 在rest-client.env.json(或127.0.0.1:80", "name": "zhangsan" }, "prod": { "host": "127.0.0.1:80", "name":"lisi" } }

    调用示例

    GET {{host}}/api/get?name={{name}}

    脚本设置环境变量

    //设置环境变量 > {% client.global.set("token", response.body.token); %}

    脚本检测

    可以对返回值进行打印,断言;

    # 登陆 POST {{host}}/system/login Content-Type: application/x-www-form-urlencoded username=admin&password=123456 > {% client.log(JSON.stringify(response.body)); client.test("Request executed successfully", function() { client.assert(response.status === 200, "Response status is not 200"); }); client.test("Response content-type is json", function() { var type = response.contentType.mimeType; client.assert(type === "application/json", "Expected 'application/json' but received '" + type + "'"); }); client.test("Request code success", function() { client.assert(response.body.code === 0, "Response code is not 0"); client.global.set("token", response.body.data); }); %} ###

    类型介绍

    • client

    client.global

    • set(varName, varValue) // 设置全局变量
    • get(varName) // 获取全局变量
    • isEmpty // 检查 global 是否为空
    • clear(varName) // 删除变量
    • clearAll // 删除所有变量
    • client.test(testName, func) // 创建一个名称为 testName 的测试
    • client.assert(condition, message) // 校验条件 condition 是否成立,否则抛出异常 message
    • client.log(text) // 打印日志
    • response
    • response.body // 字符串 或 JSON (如果 content-type 为 application/json .)
    • response.headers

    valueOf(headerName) // 返回第一个匹配 headerName 的值,如果没有匹配的返回 null
    valuesOf(headerName) // 返回所有匹配 headerName 的值的数组,如果没有匹配的返回空数组

    • response.status // Http 状态码,如: 200 / 400
    • response.contentType

    mimeType // 返回 MIME 类型,如: text/plain , text/xml , application/json .
    charset // 返回编码 UTF-8 等

    示例test.{{host}}/api/get?name={{name}} ### # POST请求 POST {{host}}/api/post/kv Content-Type: application/x-www-form-urlencoded name=zhangsan&age=11 ### # POST请求 POST {{host}}/api/post/json Content-Type: application/json referer: goframe.org/ cookie: name=zhangsan; age=11 {"name":"zhangsan","age":11} ###

    test2.{{host}}/system/user/info > {% client.log(JSON.stringify(response.body)); client.test("Request executed successfully", function() { client.assert(response.status === 404, "Response status is not 200"); }); client.test("Response content-type is json", function() { var type = response.contentType.mimeType; client.assert(type === "application/json", "Expected 'application/json' but received '" + type + "'"); }); client.test("Request code fail", function() { client.assert(response.body.code === -1, "Response code is not -1"); }); %} ### # 登陆 POST {{host}}/system/login Content-Type: application/x-www-form-urlencoded username=admin&password=123456 > {% client.log(JSON.stringify(response.body)); client.test("Request executed successfully", function() { client.assert(response.status === 200, "Response status is not 200"); }); client.test("Response content-type is json", function() { var type = response.contentType.mimeType; client.assert(type === "application/json", "Expected 'application/json' but received '" + type + "'"); }); client.test("Request code success", function() { client.assert(response.body.code === 0, "Response code is not 0"); client.global.set("token", response.body.data); }); %} ### # 登陆后访问用户信息 POST {{host}}/system/user/info token: {{token}} > {% client.log(JSON.stringify(response.body)); client.test("Request executed successfully", function() { client.assert(response.status === 200, "Response status is not 200"); }); client.test("Response content-type is json", function() { var type = response.contentType.mimeType; client.assert(type === "application/json", "Expected 'application/json' but received '" + type + "'"); }); client.test("Request code success", function() { client.assert(response.body.code === 0, "Response code is not 0"); }); %} ### # 登陆后访问用户年龄 POST {{host}}/system/user/age token: {{token}} > {% client.log(JSON.stringify(response.body)); client.test("Request executed successfully", function() { client.assert(response.status === 200, "Response status is not 200"); }); client.test("Response content-type is json", function() { var type = response.contentType.mimeType; client.assert(type === "application/json", "Expected 'application/json' but received '" + type + "'"); }); client.test("Request code success", function() { client.assert(response.body.code === 0, "Response code is not 0"); }); %} ###

    127.0.0.1:80", "name": "zhangsan" }, "prod": { "host": "127.0.0.1:80", "name":"lisi" } }

    main.go

    package main import ( "github.com/gogf/gf/frame/g" "github.com/gogf/gf/net/ggithub.com/goflyfox/tools

    gitee:gitee.com/goflyfox/tools

    如何高效使用IDEA的HTTP Client进行网络请求开发?

    教程视频

    bilibili教程地址:www.bilibili.com/video/BV12V411f7ab/

    到此这篇关于IDEA中的HTTP Client使用教程的文章就介绍到这了,更多相关IDEA HTTP Client使用内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

本文共计1205个文字,预计阅读时间需要5分钟。

如何高效使用IDEA的HTTP Client进行网络请求开发?

介绍IDEA RESTful WebServices是一个类似JMeter、Postman的工具。可以使用纯文本编辑器。官方介绍地址:https://www.jetbrains.com/help/idea/restful-webservices.。该工具是IDEA的一个组件,在‘Tools-Http Client’中。

介绍

IDEA RESTful WebServices是一个类似jmeter,postman的工具。可以使用纯文本编辑。

官网介绍地址:www.jetbrains.com/help/idea/restful-webservices.html

该工具是idea的一个组件,在Tools->Http client下;当然goland也是相同;低版本是Test Restful WebService,新版本的idea已经提示改功能废弃,建议使用new HTTP Client也就是我们此教程要介绍的工具;

示例:

创建demo1.www.baidu.com

###

点击右侧运行即可查看到结果

HTTP请求中使用变量

要在请求中提供变量,请将其括在双花括号中,如 {{variable}} 。变量名称只能包含字母,数字,下 划线符号 _ 或连字符 - 。

预定义的动态变量

每次您运行请求时,动态变量都会生成一个值: $uuid :生成通用的唯一标识符(UUID-v4) $timestamp :生成当前的UNIX时间戳 $randomInt :生成介于0到1000之间的随机整数。

GET localhost/api/get?id={{$uuid}}

创建环境变量

在项目内部,创建以下文件:

  • 在rest-client.env.json(或127.0.0.1:80", "name": "zhangsan" }, "prod": { "host": "127.0.0.1:80", "name":"lisi" } }

    调用示例

    GET {{host}}/api/get?name={{name}}

    脚本设置环境变量

    //设置环境变量 > {% client.global.set("token", response.body.token); %}

    脚本检测

    可以对返回值进行打印,断言;

    # 登陆 POST {{host}}/system/login Content-Type: application/x-www-form-urlencoded username=admin&password=123456 > {% client.log(JSON.stringify(response.body)); client.test("Request executed successfully", function() { client.assert(response.status === 200, "Response status is not 200"); }); client.test("Response content-type is json", function() { var type = response.contentType.mimeType; client.assert(type === "application/json", "Expected 'application/json' but received '" + type + "'"); }); client.test("Request code success", function() { client.assert(response.body.code === 0, "Response code is not 0"); client.global.set("token", response.body.data); }); %} ###

    类型介绍

    • client

    client.global

    • set(varName, varValue) // 设置全局变量
    • get(varName) // 获取全局变量
    • isEmpty // 检查 global 是否为空
    • clear(varName) // 删除变量
    • clearAll // 删除所有变量
    • client.test(testName, func) // 创建一个名称为 testName 的测试
    • client.assert(condition, message) // 校验条件 condition 是否成立,否则抛出异常 message
    • client.log(text) // 打印日志
    • response
    • response.body // 字符串 或 JSON (如果 content-type 为 application/json .)
    • response.headers

    valueOf(headerName) // 返回第一个匹配 headerName 的值,如果没有匹配的返回 null
    valuesOf(headerName) // 返回所有匹配 headerName 的值的数组,如果没有匹配的返回空数组

    • response.status // Http 状态码,如: 200 / 400
    • response.contentType

    mimeType // 返回 MIME 类型,如: text/plain , text/xml , application/json .
    charset // 返回编码 UTF-8 等

    示例test.{{host}}/api/get?name={{name}} ### # POST请求 POST {{host}}/api/post/kv Content-Type: application/x-www-form-urlencoded name=zhangsan&age=11 ### # POST请求 POST {{host}}/api/post/json Content-Type: application/json referer: goframe.org/ cookie: name=zhangsan; age=11 {"name":"zhangsan","age":11} ###

    test2.{{host}}/system/user/info > {% client.log(JSON.stringify(response.body)); client.test("Request executed successfully", function() { client.assert(response.status === 404, "Response status is not 200"); }); client.test("Response content-type is json", function() { var type = response.contentType.mimeType; client.assert(type === "application/json", "Expected 'application/json' but received '" + type + "'"); }); client.test("Request code fail", function() { client.assert(response.body.code === -1, "Response code is not -1"); }); %} ### # 登陆 POST {{host}}/system/login Content-Type: application/x-www-form-urlencoded username=admin&password=123456 > {% client.log(JSON.stringify(response.body)); client.test("Request executed successfully", function() { client.assert(response.status === 200, "Response status is not 200"); }); client.test("Response content-type is json", function() { var type = response.contentType.mimeType; client.assert(type === "application/json", "Expected 'application/json' but received '" + type + "'"); }); client.test("Request code success", function() { client.assert(response.body.code === 0, "Response code is not 0"); client.global.set("token", response.body.data); }); %} ### # 登陆后访问用户信息 POST {{host}}/system/user/info token: {{token}} > {% client.log(JSON.stringify(response.body)); client.test("Request executed successfully", function() { client.assert(response.status === 200, "Response status is not 200"); }); client.test("Response content-type is json", function() { var type = response.contentType.mimeType; client.assert(type === "application/json", "Expected 'application/json' but received '" + type + "'"); }); client.test("Request code success", function() { client.assert(response.body.code === 0, "Response code is not 0"); }); %} ### # 登陆后访问用户年龄 POST {{host}}/system/user/age token: {{token}} > {% client.log(JSON.stringify(response.body)); client.test("Request executed successfully", function() { client.assert(response.status === 200, "Response status is not 200"); }); client.test("Response content-type is json", function() { var type = response.contentType.mimeType; client.assert(type === "application/json", "Expected 'application/json' but received '" + type + "'"); }); client.test("Request code success", function() { client.assert(response.body.code === 0, "Response code is not 0"); }); %} ###

    127.0.0.1:80", "name": "zhangsan" }, "prod": { "host": "127.0.0.1:80", "name":"lisi" } }

    main.go

    package main import ( "github.com/gogf/gf/frame/g" "github.com/gogf/gf/net/ggithub.com/goflyfox/tools

    gitee:gitee.com/goflyfox/tools

    如何高效使用IDEA的HTTP Client进行网络请求开发?

    教程视频

    bilibili教程地址:www.bilibili.com/video/BV12V411f7ab/

    到此这篇关于IDEA中的HTTP Client使用教程的文章就介绍到这了,更多相关IDEA HTTP Client使用内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!