Golang中有哪些好用的离线Postman替代工具

Golang中有哪些好用的离线Postman替代工具 最近,我一直在寻找比 Postman 更轻量的工具。 我喜欢 Hoppscotch,但它是纯在线工具,这在我出差或网络不稳定时就成了问题。 你常用的离线 Postman 替代品是什么?最好还能支持测试,或许还能生成文档。

3 回复

布鲁诺看起来很有前景。虽然目前还有些粗糙,但其原生的集合格式对Git很友好。

更多关于Golang中有哪些好用的离线Postman替代工具的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


看看这个 VSCode 扩展

GitHub Repo

GitHub - Huachao/vscode-restclient: REST Client Extension for Visual Studio Code

REST Client Extension for Visual Studio Code

在Go生态中,有几个优秀的离线API测试工具可以替代Postman:

1. Bruno

我最常用的是Bruno,它完全离线运行,支持团队协作,且配置文件可版本控制。

# 安装Bruno
npm install -g @usebruno/cli

# 创建API集合
bruno init my-api-collection

# 运行测试
bruno test my-api-collection

Bruno使用纯文本格式存储请求,便于Git管理:

# api.bruno.yml
name: "用户API"
requests:
  - name: "获取用户列表"
    method: GET
    url: "{{baseUrl}}/users"
    auth:
      type: bearer
      token: "{{token}}"

2. Insomnia

Insomnia是功能完整的开源API客户端,支持GraphQL、gRPC和REST。

// 配合Insomnia的测试脚本示例
const axios = require('axios');

module.exports = async () => {
  const response = await axios.get('http://api.example.com/users');
  
  // 断言测试
  if (response.status !== 200) {
    throw new Error(`Expected 200, got ${response.status}`);
  }
  
  if (!Array.isArray(response.data)) {
    throw new Error('Expected array response');
  }
  
  return response.data;
};

3. HTTPie Desktop

HTTPie的命令行版本很流行,桌面版提供了GUI界面:

# 命令行版本用法
http GET https://api.example.com/users \
  Authorization:"Bearer $TOKEN" \
  Accept:"application/json"

# 保存到集合
http --offline https://api.example.com/users > request.json

4. Paw

虽然Paw是macOS专属,但它的代码生成功能很强大,可以直接生成Go代码:

// Paw生成的Go请求代码
package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {
    url := "https://api.example.com/users"
    
    req, _ := http.NewRequest("GET", url, nil)
    req.Header.Add("Authorization", "Bearer your-token-here")
    req.Header.Add("Accept", "application/json")
    
    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()
    
    body, _ := ioutil.ReadAll(resp.Body)
    fmt.Println(string(body))
}

5. 使用Go编写的工具:restic

虽然restic主要是备份工具,但其API测试模式很适合离线环境:

// 自定义简单的API测试工具
package main

import (
    "encoding/json"
    "fmt"
    "net/http"
    "os"
)

type TestCase struct {
    Name   string            `json:"name"`
    Method string            `json:"method"`
    URL    string            `json:"url"`
    Headers map[string]string `json:"headers"`
}

func runAPITest(test TestCase) error {
    req, err := http.NewRequest(test.Method, test.URL, nil)
    if err != nil {
        return err
    }
    
    for key, value := range test.Headers {
        req.Header.Set(key, value)
    }
    
    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        return err
    }
    defer resp.Body.Close()
    
    fmt.Printf("Test: %s - Status: %d\n", test.Name, resp.StatusCode)
    return nil
}

func main() {
    // 从JSON文件读取测试用例
    data, _ := os.ReadFile("tests.json")
    var tests []TestCase
    json.Unmarshal(data, &tests)
    
    for _, test := range tests {
        runAPITest(test)
    }
}

这些工具都支持离线使用,Bruno和Insomnia还提供文档生成功能。Bruno的纯文本存储特性使其特别适合与Go项目一起进行版本控制。

回到顶部