golang HTTP方法、状态码和头部快速查询工具插件库httpref的使用

Golang HTTP方法、状态码和头部快速查询工具插件库httpref的使用

概述

httpref是一个方便的HTTP参考工具,当你在命令行中需要查询HTTP相关信息时,它可以提供快速参考。这个工具特别适合那些总是记不住HTTP状态码含义、HTTP方法细节或其他HTTP相关信息的开发者。

安装

使用以下命令安装httpref:

$ go install github.com/dnnrly/httpref/cmd/httpref@latest

使用方法

按标题过滤

你可以直接查询HTTP状态码或其他信息:

$ httpref 1
  1xx  Informational response
  100  Continue
  101  Switching
  102  Processing
  103  Early hints

$ httpref 200
200 - OK

The HTTP 200 OK success status response code indicates that the request has succeeded. A 200 response is cacheable by default.

The meaning of a success depends on the HTTP request method:
    GET: The resource has been fetched and is transmitted in the message body.
    HEAD: The entity headers are in the message body.
    POST: The resource describing the result of the action is transmitted in the message body.
    TRACE: The message body contains the request message as received by the server.

The successful result of a PUT or a DELETE is often not a 200 OK but a 204 No Content (or a 201 Created when the resource is uploaded for the first time).

全文搜索

使用--search选项可以进行全文搜索,适用于头部、HTML、方法和状态参考:

$ httpref --search clear
Clear-Site-Data
  Clears browsing data (e.g. cookies, storage, cache) associated with the requesting website.
205
  Reset Content
431
  Request Header Fields Too Large

HTML元素参考

html子命令可以查找100+个活跃和已废弃的HTML元素:

$ httpref html <abbr>
<abbr>
  The Abbreviation element

The <abbr> HTML element represents an abbreviation or acronym.

When including an abbreviation or acronym, provide a full expansion of the term in plain text on first use, along with the "<abbr>" to mark up the abbreviation. This informs the user what the abbreviation or acronym means.

The optional "title" attribute can provide an expansion for the abbreviation or acronym when a full expansion is not present. This provides a hint to user agents on how to announce/display the content while informing all users what the abbreviation means. If present, "title" must contain this full description and nothing else.

$ httpref html --search anchor
<a>
  The Anchor element

The <a> HTML element (or _anchor_ element), with its "href" attribute, creates a hyperlink to web pages, files, email addresses, locations in the same page, or anything else a URL can address.

Content within each "<a>" _should_ indicate the link's destination. If the "href" attribute is present, pressing the enter key while focused on the "<a>" element will activate it.

重要make目标

httpref项目提供了一些有用的make目标:

  • deps - 下载构建、测试和发布所需的所有依赖
  • build - 构建应用程序
  • test - 运行单元测试
  • ci-test - 运行CI验证测试
  • acceptance-test - 运行验收测试
  • lint - 运行代码检查
  • update - 更新Go依赖
  • clean - 清理项目依赖
  • clean-deps - 移除所有构建依赖

Golang代码示例

以下是一个使用httpref库的简单Go示例:

package main

import (
	"fmt"
	"github.com/dnnrly/httpref"
)

func main() {
	// 查询HTTP状态码200的信息
	ref, err := httpref.Status("200")
	if err != nil {
		fmt.Printf("Error: %v\n", err)
		return
	}

	fmt.Printf("Status Code: %s\n", ref.Name)
	fmt.Printf("Description: %s\n", ref.Description)
	fmt.Printf("Spec: %s\n", ref.Spec)

	// 搜索包含"not found"的状态码
	results := httpref.SearchStatus("not found")
	fmt.Println("\nSearch results for 'not found':")
	for _, r := range results {
		fmt.Printf("%s: %s\n", r.Name, r.Description)
	}
}

这个示例展示了如何:

  1. 查询特定HTTP状态码的详细信息
  2. 搜索包含特定关键字的状态码

httpref是一个简单但强大的工具,可以帮助开发者在开发过程中快速查询HTTP相关信息,而无需离开命令行环境或打开浏览器。


更多关于golang HTTP方法、状态码和头部快速查询工具插件库httpref的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang HTTP方法、状态码和头部快速查询工具插件库httpref的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


Golang HTTP工具库httpref使用指南

httpref是一个方便的Golang库,用于快速查询HTTP方法、状态码和头部信息。它特别适合开发者在编写HTTP相关代码时快速查阅规范信息。

安装

go get github.com/dnnrly/httpref

基本使用

1. 查询HTTP方法

package main

import (
	"fmt"
	"github.com/dnnrly/httpref"
)

func main() {
	// 获取所有HTTP方法
	methods := httpref.Methods()
	for _, m := range methods {
		fmt.Printf("%s: %s\n", m.Name, m.Description)
	}

	// 查询特定方法
	getMethod, _ := httpref.Method("GET")
	fmt.Printf("\nGET方法详情:\n%s\n", getMethod.Description)
}

输出示例:

GET: 请求指定资源表示形式
HEAD: 类似于GET,但只返回状态行和头部
POST: 提交实体到指定资源
PUT: 替换目标资源的所有当前表示
DELETE: 删除指定资源
...

2. 查询HTTP状态码

func statusCodeExamples() {
	// 获取所有状态码
	statusCodes := httpref.StatusCodes()
	for _, sc := range statusCodes {
		fmt.Printf("%d %s: %s\n", sc.Code, sc.Name, sc.Description)
	}

	// 查询特定状态码
	notFound, _ := httpref.StatusCode(404)
	fmt.Printf("\n404详情:\n%s\n", notFound.Description)
}

输出示例:

200 OK: 请求成功
201 Created: 请求已完成,新资源已创建
400 Bad Request: 服务器无法理解请求
404 Not Found: 服务器找不到请求的资源
500 Internal Server Error: 服务器遇到意外情况
...

3. 查询HTTP头部

func headerExamples() {
	// 获取所有HTTP头部
	headers := httpref.Headers()
	for _, h := range headers {
		fmt.Printf("%s: %s\n", h.Name, h.Description)
	}

	// 查询特定头部
	contentType, _ := httpref.Header("Content-Type")
	fmt.Printf("\nContent-Type详情:\n%s\n", contentType.Description)
}

输出示例:

Accept: 客户端能够处理的内容类型
Content-Type: 实体主体的媒体类型
Cache-Control: 缓存指令
Authorization: 认证信息
...

高级功能

1. 按类别查询

func categoryExamples() {
	// 查询所有HTTP/2头部
	http2Headers := httpref.HeadersByCategory("http2")
	for _, h := range http2Headers {
		fmt.Printf("HTTP/2头部: %s\n", h.Name)
	}

	// 查询所有成功的状态码(2xx)
	successCodes := httpref.StatusCodesByCategory("success")
	for _, sc := range successCodes {
		fmt.Printf("成功状态码: %d %s\n", sc.Code, sc.Name)
	}
}

2. 模糊搜索

func searchExamples() {
	// 搜索包含"auth"的头部
	authHeaders := httpref.SearchHeaders("auth")
	for _, h := range authHeaders {
		fmt.Printf("认证相关头部: %s\n", h.Name)
	}

	// 搜索包含"found"的状态码
	foundStatuses := httpref.SearchStatusCodes("found")
	for _, sc := range foundStatuses {
		fmt.Printf("包含found的状态码: %d %s\n", sc.Code, sc.Name)
	}
}

实际应用示例

func httpHandlerExample(w http.ResponseWriter, r *http.Request) {
	// 检查请求方法是否允许
	allowedMethods := []string{"GET", "POST"}
	methodAllowed := false
	for _, m := range allowedMethods {
		if r.Method == m {
			methodAllowed = true
			break
		}
	}

	if !methodAllowed {
		// 获取405状态码详情
		methodNotAllowed, _ := httpref.StatusCode(405)
		w.Header().Set("Allow", strings.Join(allowedMethods, ", "))
		http.Error(w, methodNotAllowed.Description, http.StatusMethodNotAllowed)
		return
	}

	// 设置响应头
	contentType, _ := httpref.Header("Content-Type")
	w.Header().Set(contentType.Name, "application/json")
	
	// ...处理请求...
}

总结

httpref库提供了以下主要功能:

  1. 完整的HTTP方法、状态码和头部信息查询
  2. 按名称或代码快速查找特定条目
  3. 按类别过滤查询结果
  4. 模糊搜索功能
  5. 简洁易用的API设计

这个库特别适合:

  • 开发HTTP服务器或客户端时快速查阅规范
  • 编写API文档工具
  • 构建开发者辅助工具
  • 学习HTTP协议细节

通过使用httpref,开发者可以避免在编写HTTP相关代码时频繁查阅外部文档,提高开发效率。

回到顶部