开心波罗蜜

妙无立,无所不立!

  • 主页
  • 相册
  • 生活
  • 佛法
  • 工作
所有文章 友链 关于我

开心波罗蜜

妙无立,无所不立!

  • 主页
  • 相册
  • 生活
  • 佛法
  • 工作

Intellj IDEA 插件 HTTP Client

2019-04-08

image.png

IntelliJ IDEA 插件 HTTP Client

原文地址:https://github.com/corningsun/yuchigong/blob/httpClient/httpClient/README.md

资料链接

ID NAME DESCROPTION
0 IDEA Plugins https://www.jetbrains.com/help/idea/testing-restful-web-services.html
1 IDEA Code Editor https://www.jetbrains.com/help/idea/http-client-in-product-code-editor.html

界面客户端

  • 使用手册

https://www.jetbrains.com/help/idea/testing-restful-web-services.html

  • Tools -> HTTP Client -> Test RESTful Web Service

文本客户端

  • 使用手册

https://www.jetbrains.com/help/idea/http-client-in-product-code-editor.html

特点

  • 纯文本编写
  • 支持统一配置
  • 支持 scripts 脚本

创建新的请求文件

  • Scratch files (全局文件)
  • physical files(项目文件)

live templates

  • ‘gtrp’ and ‘gtr’ create a GET request with or without query parameters;
  • ‘ptr’ and ‘ptrp’ create a POST request with a simple or parameter-like body;
  • ‘mptr’ and ‘fptr’ create a POST request to submit a form with a text or file field (multipart/form-data);
  • Live Templates 演示

  • Live Templates 配置

支持 HTTP 1.1 所有方法

POST、GET、PUT、DELETE、HEAD、OPTIONS、TRACE、CONNECT

  • GET
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
### Get request with a header
GET https://httpbin.org/ip
Accept: application/json

### Get request with parameter
GET https://httpbin.org/get?show_env=1
Accept: application/json

### Get request with environment variables
GET {{host}}/get?show_env={{show_env}}
Accept: application/json

### Get request with disabled redirects
# @no-redirect
GET http://httpbin.org/status/301
  • POST
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
### Send POST request with json body
POST https://httpbin.org/post
Content-Type: application/json

{
"id": 999,
"value": "content"
}

### Send POST request with body as parameters
POST https://httpbin.org/post
Content-Type: application/x-www-form-urlencoded

id=999&value=content

### Send a form with the text and file fields
POST https://httpbin.org/post
Content-Type: multipart/form-data; boundary=WebAppBoundary

--WebAppBoundary
Content-Disposition: form-data; name="element-name"
Content-Type: text/plain

Name
--WebAppBoundary
Content-Disposition: form-data; name="data"; filename="data.json"
Content-Type: application/json

< ./request-form-data.json
--WebAppBoundary--

###

演示项目简介

  • Spring Boot - Spring Security
  • 首页 http://localhost:8080/index.html
  • 登陆页面 http://localhost:8080/login.html
  • 测试接口(有登陆验证) http://localhost:8080/api/security/test
  • 测试账号,用户名: lee,密码:123456
  • 用浏览器直接访问测试接口会被强制跳转到登陆页面,登陆成功后返回接口调用结果。
  • 原理是登陆校验成功返回 JSESSIONID,客户端将 JSESSIONID 存到 Cookie ,服务端根据 Cookie 验证是否登陆。

演示项目接口调用实战

登陆接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
POST http://localhost:8080/api/login
Content-Type: application/x-www-form-urlencoded

account=lee&password=123456

// 返回
HTTP/1.1 302
Set-Cookie: JSESSIONID=A323D83D5E062588F4ACA433E891EA67; Path=/; HttpOnly
Location: http://localhost:8080/index.html
... 略

测试访问 无 Cookie
GET http://localhost:8080/api/security/test
Accept: application/json

GET http://localhost:8080/api/security/test
Accept: application/json
Cookie: JSESSIONID=1C1DD3EB60DEE60664FB0BFE0F1C9942

测试接口

1
2
3
4
5
6
7
8
GET http://localhost:8080/api/security/test
Accept: application/json
Cookie: JSESSIONID=A323D83D5E062588F4ACA433E891EA67

// 返回
HTTP/1.1 200

you have been authenticated

查看请求历史

  • 点击右上角的按钮 Show HTTP Requests History

  • Tools | HTTP Client | Show HTTP Requests History

演示接口重构 - 统一配置

通用配置,域名/端口

  • rest-client.env.json 或 http-client.env.json
1
2
3
4
5
6
7
8
{
"default": {

},
"local": {
"host": "http://localhost:8080"
}
}

个人安全配置,用户名/密码

  • rest-client.private.env.json 或 http-client.private.env.json
1
2
3
4
5
6
7
8
9
{
"default": {

},
"local": {
"account": "lee",
"password": "123456"
}
}

重构后的请求文件

1
2
3
4
5
6
7
8
9
10
11
12
### 登陆
POST {{host}}/api/login
Content-Type: application/x-www-form-urlencoded

account=lee&password=123456

### 测试接口
GET {{host}}/api/security/test
Accept: application/json
Cookie: JSESSIONID=1C1DD3EB60DEE60664FB0BFE0F1C9942

###

运行请求

点击运行按钮,可以选择对应的环境

使用 response handler scripts

引用方式

  • 直接引用
1
2
3
4
5
6
GET host/api/test

> {%
// Response Handler Script
...
%}
  • 文件引用
1
2
3
GET host/api/test

> scripts/my-script.js

主要方法

HTTP Response handling API reference

  • 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 等
  • 方法调用示例
1
2
3
4
5
6
7
GET https://httpbin.org/status/200

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

演示接口重构 - 动态更新 Cookie

1
2
3
4
5
6
7
8
9
10
11
12
### 登陆
POST {{host}}/api/login
Content-Type: application/x-www-form-urlencoded

account={{account}}&password={{password}}

> {% client.global.set("Set-Cookie", response.headers.valueOf("Set-Cookie")) %}

### 测试接口
GET {{host}}/api/security/test
Accept: application/json
Cookie: {{Set-Cookie}}

演示接口重构 - 测试接口返回

1
2
3
4
5
6
7
8
9
10
11
12
13
14
### 登陆
POST {{host}}/api/login
Content-Type: application/x-www-form-urlencoded

account={{account}}&password={{password}}

> {% client.global.set("Set-Cookie", response.headers.valueOf("Set-Cookie")) %}

### 测试接口
GET {{host}}/api/security/test
Accept: text/plain
Cookie: {{Set-Cookie}}

> login_demo_4_js_test.js
  • login_demo_4_js_test.js
1
2
3
4
5
client.test("Request executed successfully", function () {
client.assert(response.status === 200, "Response status is not 200");
client.log("response.body=" + response.body);
client.assert(response.body === 'you have been authenticated', "Response body check fail");
});

  • HTTP
  • 工作

扫一扫,分享到微信

微信分享二维码
MAC工具
enable multi-tenancy on openstack pike
© 2019 开心波罗蜜
Hexo Theme Yilia by Litten
  • 所有文章
  • 友链
  • 关于我

tag:

  • Ansible
  • Storage
  • Ceph
  • Bigdata
  • 原创
  • OpenStack
  • ELK
  • Linux
  • 监控
  • Docker
  • Network
  • Loadbalancer
  • SHELL
  • Mac
  • LB
  • coding
  • KVM
  • ironic
  • Neutron
  • SDN
  • NetWork
  • 禅净密
  • Libvirt
  • USB
  • MCOS
  • Kolla
  • Cinder
  • 运维
  • VIP
  • 生活
  • Git
  • GlusterFS
  • HTTP
  • cate美食
  • OpenShift
  • pass

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • 友情链接1
  • 友情链接2
  • 友情链接3
  • 友情链接4
  • 友情链接5
  • 友情链接6
妙无立,无不可立.