专栏首页吴伟祥IDEA中的HTTP Client Editor测试API

IDEA中的HTTP Client Editor测试API

在前后端分离项目,前后端通过api进行通信。如果用postman免费版进行api测试的话,由于无法保存测试脚本到文件,不方便前端查看。

你可以选择付费版。也可以利用IDEA自带的HTTP Client Editor编写测试脚本。这里写个demo主要是方便查询语法。

HTTP请求

### 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

###

POST请求

### 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--

###

认证请求

### Basic authorization.
GET https://httpbin.org/basic-auth/user/passwd
Authorization: Basic user passwd

### Basic authorization with variables.
GET https://httpbin.org/basic-auth/user/passwd
Authorization: Basic {{username}} {{password}}

### Digest authorization.
GET https://httpbin.org/digest-auth/realm/user/passwd
Authorization: Digest user passwd

### Digest authorization with variables.
GET https://httpbin.org/digest-auth/realm/user/passwd
Authorization: Digest {{username}} {{password}}

### Authorization by token, part 1. Retrieve and save token.
POST https://httpbin.org/post
Content-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/headers
Authorization: Bearer {{auth_token}}

###

测试响应

### Successful test: check response status is 200
GET 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 200
GET 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-type
GET 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 body
GET https://httpbin.org/get

> {%
client.test("Headers option exists", function() {
  client.assert(response.body.hasOwnProperty("headers"), "Cannot find 'headers' option in response");
});
%}

###

上面这些脚本是很好的demo,涵盖了语法,并且可以直接点击运行。

本文转载自:https://my.oschina.net/hutaishi/blog/1859073

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • spring如何给静态变量注入值 原

    wuweixiang
  • 微信开发 原

    微信公众平台是运营者通过公众号为微信用户提供资讯和服务的平台,而公众平台开发接口则是提供服务的基础,开发者在公众平台网站中创建公众号、获取接口权限后,可以通过阅...

    wuweixiang
  • Mybatis 3.5新特性——Optional支持

    Mybatis 3.5 发布有段时间了,终于支持了 Optional ,这么实用的特性,竟然还没人安利……于是本文出现了。

    wuweixiang
  • springmvc之RequestMapping注解既可以修饰类也可以修饰方法

    绝命生
  • 必应API接口node.js版 - 极客玩家大白

    近期,在研究百度、必应、API等的url提交API时,发现有用Go语言做工具的大佬的分享 利用 API 自动向搜索引擎提交网址(Go语言版) - py...

    Enjoy233
  • 安卓适配

    在开发的时候,发现红米一直安装不上APP。经过百度查询,发现是在开发设置中打开了启用MIUI优化设置,将其关掉,然后重启,然后接着开启USB安装,之前因为一直没...

    魔王卷子
  • 教你如何优雅的用R语言调用有道翻译

    最近刚发现了个有趣的包,一个R语言发烧友开发了R语言与有道在线翻译的接口,可能这位大神也是一个受够了每天打开网页狂敲键盘查词的罪,索性自己动手,从此丰衣足食。 ...

    数据小磨坊
  • 迁移学习之通览

    摘要: 深度学习作为机器学习的重要领域,在过去的几年时间里面发挥了巨大的作用。但是随着机器学习在不同领域的深入应用,迁移学习正在成为不可忽视的力量。 ? 我们...

    IT派
  • 邮件服务器 Postfix+ Cyrus-SASL+cyrus-IMAPD+日常维护

    一,安装Postfix postfix-2.6.5.tar.gz postfix-2.6.5-vda-ng.patch.gz 二,rpm包安装Cyrus-SAS...

    三杯水Plus
  • 简单的统计学:如何用Python计算扑克概率

    在本文中,我们展示了如何在Python中表示基本的扑克元素,例如“手”和“组合”,以及如何计算扑克赔率,即在无限额德州扑克中获胜/平局/失败的可能性。

    deephub

扫码关注云+社区

领取腾讯云代金券