云计算、AI、云原生、大数据等一站式技术学习平台

网站首页 > 教程文章 正文

VS Code 使用插件rest client实现API请求替代postman

jxf315 2025-04-09 17:13:02 教程文章 21 ℃

rest client 是一款简单使用的api调试工具,相比postman更轻便,完全免费。postman限制比较多,强制要求登录,这点令人不爽,高级功能还要收费。

安装

在“扩展”搜索“rest client”



简单使用

创建一个后缀为.http.rest 文件

格式:

  1. 多个请求以### 分割
  2. header部分和body部分用空白行分割


示例:


### Get
GET http://localhost:8080
?name=aaaa
&id=123 HTTP/1.1
Authorization: Bearer your_access_token


### 默认post提交
POST http://localhost:8080 HTTP/1.1
Content-Type:application/x-www-form-urlencoded

name=aaaa
&id=2222

### json post提交
POST http://localhost:8080 HTTP/1.1
Content-Type:application/json

{
    "id":111,
    "name":"dd"
}

### 上传文件
POST http://localhost:8080/upload
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain

< ./test.txt
------WebKitFormBoundary7MA4YWxkTrZu0gW--

注:

1、
Content-Type:application/x-www-form-urlencoded请求,header和body中间仅为一个空白行,多了会有问题,建议统一都是一个空白行。

2、GET参数可以分行

GET http://localhost:8080
?name=aaaa
&id=123 HTTP/1.1
Authorization: Bearer your_access_token

3、boundary :用于分隔消息体中的不同部分

点击“Send Request” 发起请求



环境变量

  1. 自定义变量

示例:

@url = http://localhost:8080
### Get
GET {{url}}/api
?name=aaaa
&id=123 HTTP/1.1
Authorization: Bearer your_access_token
  1. 全局变量

在左下角“设置”

输入:rest-client:environments


点击“在settings.json中编辑 ”

在 "
rest-client.environmentVariables" 中写入

    "rest-client.environmentVariables": 
    {
        "$shared": {
            "version": "v1"
        },
        "production": {
            "baseUrl": "http://api.example.com"
        },
        "local": {
            "baseUrl": "http://127.0.0.1"
        }
    }

在.http文件中的引用

### Get
GET {{baseUrl}}/api
?name=aaaa
&id=123 HTTP/1.1
Authorization: Bearer your_access_token

切换环境

点击“No Environment”

这时会弹出环境列表,选择想要切换的环境

快捷方式

查看restclient相关的快捷键

最近发表
标签列表