再也不需要Postman了!有了它REST请求统统搞定,效率翻倍.md

 

转载weixin_33769125 最后发布于2019-03-19 15:35:30 阅读数 248  收藏

展开

做开发或者测试,天天可能需要调用REST接口联调或者测试,查看数据返回是否正确、查看返回的JSON格式、格式化JSON,看似简单的事情,做起来很费事,而且烦人!看见很多同事使用最多的就是Postman和curl操作了,先看看这两种操作方式。

postman发请求

  • 下载postman应用
  • 配置接口,设置参数
  • 多个电脑使用可能还需要使用同一个账号同步

curl发请求

  • 写先好curl命令
  • 复制到终端执行
  • 对response JSON格式化

上面两种方式给我的感觉是都很麻烦,接口请求参数多了配置很麻烦、总是在复制来复制去,耗时、跨平台不方便、不方便保存、不方便分享等等。

期望有一种简单方便的请求参数配置,跨平台,方便保存,方便查看请求结果,能够够集成到多种IDE,方便开发和测试使用。

接下来介绍两大神奇,分别是 VSCodeREST Client和jetbrains家族HTTP Client in IntelliJ IDEA Code Editor,毫不夸张的说,如果你正在使用VSCode或者jetbrains家族的IDE,有了他们让你的工作效率大大提高,并且你会深深的爱上他。

在VSCode中使用REST Client

REST Client支持cURL 和 RFC 2616 两种标准来调用REST API, 使用起来非常简单,只需要写一个以.http或者 .rest 结尾命令的的文件即可实现调用。

首先来看个简单例子,这里有一个GET接口(httpbin.org/ip)看看如何调用。

注意: httpbin.org是一个开源的接口测试网站,它能测试 HTTP 请求和响应的各种信息,比如 cookie、ip、headers 和登录验证等,且支持 GET、POST 等多种方法,对 web 开发和测试很有帮助。httpbin.org/

  • 首先在VSCode中创建一个名叫test.http(下载地址:test.http)的文件,然后加入以下代码
 
  1. ### Get request with a header

  2. GET https://httpbin.org/ip

  3. Accept: application/json

  4.  
  5. ### Get request with a header

  6. curl -H "Content-Type:application/json" -XGET 'https://httpbin.org/ip'

  7. 复制代码

 

 

 

  • 发送请求 发送请求非常简单,只需要点击上图中的Send Request即可执行,最终得到结果如下

 

 

 

  • 查看结果 执行返回后,HTTP的状态信息和header都在右侧,并且对body已经格式化好了,是不是非常方便,真的是太方便了。

可以看到上面对同一个接口调用有两种调用方式,其执行结构都是一样,即使你写的再复杂,参数再多的curl请求,拿过来保存在这里直接执行即可,比如下面这样一个例子(直接copy我的一个本地测试),直接copy到test.http这个文件中执行即可,并且还能够美观的查看执行结果。

 
  1. curl -H "Content-Type:application/json" -XPUT 'http://localhost:8083/connectors/test-connector/config' -d '

  2. {

  3. "connector.class": "io.debezium.connector.mysql.MySqlConnector",

  4. "tasks.max": "1",

  5. "database.hostname": "localhost",

  6. "database.port": "3306",

  7. "database.user": "root",

  8. "database.password": "password",

  9. "database.server.id": "19991",

  10. "database.server.name": "trade_order",

  11. "database.whitelist": "db_order",

  12. "include.schema.changes": "false",

  13. "snapshot.mode": "schema_only",

  14. "snapshot.locking.mode": "none",

  15. "database.history.kafka.bootstrap.servers": "localhost:9092",

  16. "database.history.kafka.topic": "dbhistory.trade_order",

  17. "decimal.handling.mode": "string",

  18. "table.whitelist": "db_order.t_order_item",

  19. "database.history.store.only.monitored.tables.ddl":"true",

  20. "database.history.skip.unparseable.ddl":"true"

  21. }'

  22. 复制代码

常用写法

REST Client的写法非常简单,你只需要知道HTTP请求的构成就行,分别是Query StringsRequest HeadersRequest Body,只需要看一个例子就会写所有的,更复杂的写法查看REST Client Overview,常见构成结构如下。

 
  1. 请求方法 地址

  2. header

  3.  
  4. request body

  5. 复制代码

例如:

 
  1. ### Send POST request with json body

  2. POST https://httpbin.org/post?client=ios&name=哈哈哈

  3. Content-Type: application/json

  4. myHeader: myheader

  5. auth-token: mytoken

  6.  
  7. {

  8. "id": 999,

  9. "value": "content"

  10. }

  11. 复制代码

掌握这一个例子适用于大部分场景就够了,当然了这里只是一个介绍,REST Client还支持好多功能,非常优秀,非常好用,简直是爱不释手,官网文档都有REST Client

在IntelliJ中使用HTTP Client in IntelliJ IDEA Code Editor

在中使用HTTP Client in IntelliJ IDEA Code Editor和在VSCode中使用REST Client一样,唯一的区别就是IntelliJ IDE暂时不支持curl的方式。

附:常见例子

Send POST request with json body

 
  1. POST https://httpbin.org/post

  2. Content-Type: application/json

  3.  
  4. {

  5. "id": 999,

  6. "value": "content"

  7. }

  8. 复制代码

Send POST request with body as parameters

 
  1. POST https://httpbin.org/post

  2. Content-Type: application/x-www-form-urlencoded

  3.  
  4. id=999&value=content

  5. 复制代码

Send a form with the text and file fields

 
  1. POST https://httpbin.org/post

  2. Content-Type: multipart/form-data; boundary=WebAppBoundary

  3.  
  4. --WebAppBoundary

  5. Content-Disposition: form-data; name="element-name"

  6. Content-Type: text/plain

  7.  
  8. Name

  9. --WebAppBoundary

  10. Content-Disposition: form-data; name="data"; filename="data.json"

  11. Content-Type: application/json

  12.  
  13. < ./request-form-data.json

  14. --WebAppBoundary--

  15. 复制代码

Basic authorization.

 
  1. GET https://httpbin.org/basic-auth/user/passwd

  2. Authorization: Basic user passwd

  3. 复制代码

Basic authorization with variables.

 
  1. GET https://httpbin.org/basic-auth/user/passwd

  2. Authorization: Basic {{username}} {{password}}

  3. 复制代码

Digest authorization.

 
  1. GET https://httpbin.org/digest-auth/realm/user/passwd

  2. Authorization: Digest user passwd

  3. 复制代码

Digest authorization with variables.

 
  1. GET https://httpbin.org/digest-auth/realm/user/passwd

  2. Authorization: Digest {{username}} {{password}}

  3. 复制代码

Authorization by token, part 1. Retrieve and save token.

 
  1. POST https://httpbin.org/post

  2. Content-Type: application/json

  3.  
  4. {

  5. "token": "my-secret-token"

  6. }

  7.  
  8. > {% client.global.set("auth_token", response.body.json.token); %}

  9. 复制代码

Authorization by token, part 2. Use token to authorize.

 
  1. GET https://httpbin.org/headers

  2. Authorization: Bearer {{auth_token}}

  3. 复制代码

总结

如果你正在使用VSCode或者IntelliJ IDE一定要使用这两款优秀的插件,让你的工作方便省事,方便发送请求,方便查看执行结构,方便保存,方便分享。

转载于:https://juejin.im/post/5c910a616fb9a070f30afcc6

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值