• [go 面试] Go Kit中读取原始HTTP请求体的方法


    在Go Kit中,如果你想读取未序列化的HTTP请求体,可以使用标准的net/http包来实现。以下是一个示例,演示了如何完成这个任务:

    package main
    
    import (
    	"context"
    	"encoding/json"
    	"errors"
    	"fmt"
    	"io/ioutil"
    	"net/http"
    
    	"github.com/go-kit/kit/transport/http"
    )
    
    func main() {
    	http.Handle("/your-endpoint", http.NewServer(
    		yourEndpoint,
    		decodeRequest,
    		encodeResponse,
    	))
    }
    
    // 请求和响应类型
    type YourRequest struct {
    	// 定义你的请求结构
    	// ...
    }
    
    type YourResponse struct {
    	// 定义你的响应结构
    	// ...
    }
    
    // 你的端点逻辑
    func yourEndpoint(ctx context.Context, request interface{}) (interface{}, error) {
    	// 获取原始请求体
    	rawBody, ok := request.(json.RawMessage)
    	if !ok {
    		return nil, errors.New("无法访问原始请求体")
    	}
    
    	// 根据需要处理原始请求体
    	fmt.Println("原始请求体:", string(rawBody))
    
    	// 你的实际端点逻辑在这里
    	// ...
    
    	// 返回响应(示例响应)
    	return YourResponse{Message: "请求成功处理"}, nil
    }
    
    // 请求解码器以获取原始请求体
    func decodeRequest(_ context.Context, r *http.Request) (interface{}, error) {
    	// 读取原始请求体
    	body, err := ioutil.ReadAll(r.Body)
    	if err != nil {
    		return nil, err
    	}
    
    	// 将原始请求体作为json.RawMessage返回
    	return json.RawMessage(body), nil
    }
    
    // 响应编码器
    func encodeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error {
    	return json.NewEncoder(w).Encode(response)
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    在这个例子中:

    • decodeRequest 函数使用 ioutil.ReadAll 读取原始的HTTP请求体,然后将其作为 json.RawMessage 返回。
    • 在 yourEndpoint 函数中,通过将请求类型断言为 json.RawMessage,你可以访问原始的请求体,然后根据需要处理它。
    • 代码的其余部分设置了一个基本的Go Kit HTTP服务器,包括你的端点、请求解码和响应编码逻辑。
      记得用你实际的请求和响应类型,以及你的用例需要的处理逻辑替换占位符类型和端点逻辑。

    示例

    package main
    
    import (
    	"context"
    	"encoding/json"
    	"fmt"
    	"net/http"
    
    	"github.com/go-kit/kit/endpoint"
    	"github.com/go-kit/kit/log"
    	"github.com/go-kit/kit/transport/http"
    )
    
    // 表示请求负载的结构体
    type Request struct {
    	Message string `json:"message"`
    }
    
    // 表示响应负载的结构体
    type Response struct {
    	Result string `json:"result"`
    }
    
    func main() {
    	// 创建一个简单的Go Kit服务
    	var svc MyService
    	endpoint := makeUppercaseEndpoint(&svc)
    
    	// 创建一个Go Kit HTTP传输
    	httpHandler := http.NewServer(
    		endpoint,
    		decodeRequest,
    		encodeResponse,
    	)
    
    	// 启动HTTP服务器
    	http.ListenAndServe(":8080", httpHandler)
    }
    
    // MyService是一个只有一个方法的简单服务
    type MyService struct{}
    
    // Uppercase是MyService上的一个方法
    func (MyService) Uppercase(ctx context.Context, message string) (string, error) {
    	return fmt.Sprintf("接收到消息:%s", message), nil
    }
    
    // makeUppercaseEndpoint是创建Uppercase方法的Go Kit端点的辅助函数
    func makeUppercaseEndpoint(svc MyService) endpoint.Endpoint {
    	return func(ctx context.Context, request interface{}) (interface{}, error) {
    		req := request.(Request)
    		result, err := svc.Uppercase(ctx, req.Message)
    		return Response{Result: result}, err
    	}
    }
    
    // decodeRequest是解码传入JSON请求的辅助函数
    func decodeRequest(_ context.Context, r *http.Request) (interface{}, error) {
    	var request Request
    	if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
    		return nil, err
    	}
    	return request, nil
    }
    
    // encodeResponse是编码传出JSON响应的辅助函数
    func encodeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error {
    	return json.NewEncoder(w).Encode(response)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69

    在这个例子中,decodeRequest 函数是一个解码传入JSON请求的辅助函数,makeUppercaseEndpoint 函数是一个创建Uppercase方法的Go Kit端点的辅助函数。这个示例演示了如何使用Go Kit处理HTTP请求和响应。记得根据你的具体用例和要求对其进行调整。

  • 相关阅读:
    excel导入存到数据库(老项目 poi)
    接口测试——HtmlUnit、OkHttp
    后管实现面包屑功能
    【BLIP/BLIP2/InstructBLIP】一篇文章快速了解BLIP系列(附代码讲解说明)
    七甲川染料CY7标记PCL载药纳米粒|CY7-PCL|PCL-CY7|CY7-SS-PEG-PCL(齐岳)
    python的环境安装(版本3.10.6)
    java导出word实现
    [12] 使用 CUDA 加速排序算法
    VMware16.1.2安装及密钥
    复杂逻辑的开发利器—Mendix快速实现AQL质量抽检
  • 原文地址:https://blog.csdn.net/ldxxxxll/article/details/136448902