使用idea、pycharm、golang等工具的REST Client测试Api

1、使用rest client

tools>http client>test restful web service
在这里插入图片描述
在这里插入图片描述

2、测试请求

2.1 get请求

选择请求方式,填写地址和端口、请求地址,请求参数写在request parameters中,然后点击左侧三角运行。
在这里插入图片描述
响应
在这里插入图片描述
响应包含了响应状态码、响应时间、数据等信息。

2.2 post请求

这个我用的不好。。。。所以我使用的是下面的用.http文件的方式自己写。。
在这里插入图片描述

3、使用.http文件的方式来调用

如在项目根目录下新建一个scratch.http文件,名称可以自定义。
在这里插入图片描述
点击该文件,发现该文件上方出现了一个工具条
在这里插入图片描述

  • Add Request:在该文件中添加一个请求,可选的有get、post
  • Add Environment File:可选项regular、private,根据选择的不同在项目根目录下创建的文件名称也不同。然后在运行时可以选择不同的环境来进行。环境切换
    • regular 对应的文件http-client.env.json
    • private 对应的文件http-client.private.env.json
  • Convert from cURL:会把curl命令转换为具体的请求添加到该文件中,没用过。。在这里插入图片描述
  • Open Log:将你的每一次请求记录下来,也就是查看历史记录的。
  • Examples:就是请求示例、选择对应的请求方式(get、post),出现该请求方式的示例。与Add Requuest稍有不同,前者是打开示例,不添加请求在文件中,后者不仅有示例而且在你新建的文件中添加。

3.1 get请求

在文件中编写如下内容:

GET http://localhost:3000/api/v1/users/?page=1&size=2
Accept: */*
Cache-Control: no-cache
Content-Type: application/json

在这里插入图片描述
点击左侧绿色三角即可运行。

3.1 post请求

POST http://localhost:3000/api/v1/users/
Accept: */*
Cache-Control: no-cache
Content-Type: application/json

{
  "username": "zhangsan2",
  "password": "useruser",
}

3.2 post请求(上传文件)

POST http://localhost:3000/api/v1/upload
Content-Type: multipart/form-data; boundary=WebAppBoundary

--WebAppBoundary
Content-Disposition: form-data; name="file"; filename="5820a5259bb24d1cbf865a7a8638402a.jpg"

< C:/Users/cf/Pictures/5820a5259bb24d1cbf865a7a8638402a.jpg
--WebAppBoundary--

在这里插入图片描述

3.3 put请求

PUT http://localhost:3000/api/v1/categorys/3/
Accept: */*
Cache-Control: no-cache
Content-Type: application/json

{
  "name": "幽默"
}

在这里插入图片描述

3.4 delete请求

DELETE http://localhost:3000/api/v1/categorys/3/
Accept: */*
Cache-Control: no-cache
Content-Type: application/json

在这里插入图片描述

4、环境切换

1、在http-client.env.json中写入如下内容

{
  "development": {
    "host": "http://127.0.0.1:8090",
    "authorization": ""
  },
  "test": {
    "host": "http://127.0.0.1:8011",
    "authorization": ""
  },
  "release": {
    "host": "http://gateway.xxx.cn/",
    "authorization": ""
  }
}

在这里插入图片描述

然后回到.http文件中,将原先写死的地址可以用刚才设置的变量替换掉。
在这里插入图片描述
运行某个接口时,可以根据环境选择。
在这里插入图片描述
2、http-client.private.env.json文件,来设置一些隐式数据,如用户名、密码等

{
	"development": {
		"username": "zhangsan2",
		"password": "useruser"
	},
	"test": {
		"username": "zhangsan1",
		"password": "useruser"
	}
}

3、用我们设置的来请求

POST {{host}}/api/v1/login
Accept: */*
Cache-Control: no-cache
Content-Type: application/json

{
  "username": "{{username}}",
  "password": "{{password}}"
}

在这里插入图片描述
这样在请求登录接口时,运行development环境时,用户名是zhangsan2,密码是useruser,运行test环境时,用户名是zhangsan1,密码是useruser。

5、执行历史结果

每次执行都会存储执行结果,会在.idea/httpRequests/下面生成json文件。
在这里插入图片描述
可以对历史执行结果进行比较,打开一个文件,按快捷键ctrl+D,选择另一个文件,查看文件之间的不同。
在这里插入图片描述

6、查看历史请求

编辑器会保留最近50个执行请求,他们都被存储在.idea/httpRequests/http-requests-log.http(上面的open log打开的也是这个文件)中,里面有50个请求几率,你可以看到之前的执行请求。你也可以在这个文件里面执行之前的请求。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值