首页 > 编程知识 正文

IntelliJ IDEA 自带的 HTTP Client 接口调用插件,吊打 Postman

时间:2023-05-06 21:18:56 阅读:52419 作者:3295

我是陈皮。 在网络编码的ITer上,通过微信搜索“陈皮的JavaLib”,第一时间阅读最新文章,回复【资料】,我就仔细整理了一下技术

文章1前言2创建http客户端2.1 http客户端文件2.2 http客户端特性2.3请求2.4如何创建请求方法

前言开发调试Web服务时,需要进行接口的调用测试。 或者,如果要将第三方系统与坞站连接,则必须调用远程第三方接口进行协作。 此时,我相信大家首选的工具一般是Postman。 是目前流行、功能丰富的接口调用调试工具。 如下所示。

但是,我们通常使用IntelliJ IDEA代码编辑器来开发和调试Web服务。 使用Postman工具测试界面,不仅需要在电脑上安装Postman,还需要在不同的工具之间切换,很麻烦。 幸运的是,IDEA附带了一个简单、轻量级的接口调用插件和http客户端。

2 http客户端http客户端是IDEA拥有的一个简单、轻量级的接口调用插件,通过它可以在IDEA上开发、调试和测试rest风格的web服务。

注:请确保http客户端插件已安装并默认安装。 如果未安装,请使用文件设置插件路径安装:

要创建2.1 http客户端文件,可以创建两种文件类型的http客户端文件。 一个是临时文件(scratch files,不与项目绑定),另一个是非临时文件(physical files,与项目项目绑定)。

如果要创建的http客户端文件用于临时调用接口测试,并且不需要保留记录以便以后使用,请使用临时文件。 建议将记录请求参数、请求结果等归档,以便以后继续使用,或者与项目一起提交到远程git仓库时,使用非临时文件。

创建 HTTP Client 临时文件

打开后,显示界面与Postman大致相同,但这种样式的界面已过时,官方不建议使用。 最新版本的IDEA中已经没有了。

官方建议使用编码样式的接口。 单击上一个界面顶部的“Convert request to the new format”将打开新的http客户端界面。

创建 HTTP Client 非临时文件

可以在项目根目录下创建用于存储请求文件的文件夹,然后按如下方式创建干净的历史http客户端请求文件:

2.2 HTTP客户端属性HTTP请求存储在后缀为. http或. rest的文件中,并带有小API图标。

请求文件可以包含多个请求,多个请求之间用三个散列号###分隔。 对于临时文件,每次运行请求时,都会在请求下生成与请求结果对应的文件链接,您可以按Ctrl鼠标左键将其打开。

的所有请求结果、请求历史记录、cookies等信息存储在. idea文件夹下,如下所示:

2.3创建请求如何使用右上角的快捷按钮创建请求。 可以选择不同方法的请求,如下所示:

使用快捷键创建请求。 例如,输入gtr可以快速创建简单的获取请求,如下所示:

使用Ctrl J快捷键,可以查看创建HTTP请求的所有快捷键,如下所示:

当您从cURL创建请求,单击右上角的“Convert form cURL”按钮,然后输入cURL地址时,它将自动转换为:

2.4请求方式GET

# # # getrequestwithaheaderget https://http bin.org/IP accept 3360应用程序/JSON # # getrequestwithparameterget 3359 htpbittp STP JSON # # getrequestwithenvironmentvariablesget { {主机} }/get? show _ env={ show _ env } accept : application/JSON # # getrequestwithdisabledredirects # @ no-redirect get 33555 getrequest id={

{$uuid}}&ts={{$timestamp}}###

POST

### Send POST request with json bodyPOST https://httpbin.org/postContent-Type: application/json{ "id": 999, "value": "content"}### Send POST request with body as parametersPOST https://httpbin.org/postContent-Type: application/x-www-form-urlencodedid=999&value=content### Send a form with the text and file fieldsPOST https://httpbin.org/postContent-Type: multipart/form-data; boundary=WebAppBoundary--WebAppBoundaryContent-Disposition: form-data; name="element-name"Content-Type: text/plainName--WebAppBoundaryContent-Disposition: form-data; name="data"; filename="data.json"Content-Type: application/json< ./request-form-data.json--WebAppBoundary--### Send request with dynamic variables in request's bodyPOST https://httpbin.org/postContent-Type: application/json{ "id": {{$uuid}}, "price": {{$randomInt}}, "ts": {{$timestamp}}, "value": "content"}###

PUT

PUT http://localhost:8080/person/putContent-Type: application/json{"name": "陈皮","age": 17}

PATCH

###PATCH http://localhost:8080/person/putContent-Type: application/json{"name": "陈皮","age": 17}

鉴权方式

### Basic authorization.GET https://httpbin.org/basic-auth/user/passwdAuthorization: Basic user passwd### Basic authorization with variables.GET https://httpbin.org/basic-auth/user/passwdAuthorization: Basic {{username}} {{password}}### Digest authorization.GET https://httpbin.org/digest-auth/realm/user/passwdAuthorization: Digest user passwd### Digest authorization with variables.GET https://httpbin.org/digest-auth/realm/user/passwdAuthorization: Digest {{username}} {{password}}### Authorization by token, part 1. Retrieve and save token.POST https://httpbin.org/postContent-Type: application/json{ "token": "my-secret-token"}> {% client.global.set("auth_token", response.body.json.token); %}### Authorization by token, part 2. Use token to authorize.GET https://httpbin.org/headersAuthorization: Bearer {{auth_token}}###

断言方式

### 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");});%}### Failed test: check response status is 200GET https://httpbin.org/status/404> {%client.test("Request executed successfully", function() { client.assert(response.status === 200, "Response status is not 200");});%}### Check response status and content-typeGET https://httpbin.org/get> {%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 + "'");});%}### Check response bodyGET https://httpbin.org/get> {%client.test("Headers option exists", function() { client.assert(response.body.hasOwnProperty("headers"), "Cannot find 'headers' option in response");});%}###

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。