| 12345678910111213141516171819202122232425262728293031323334353637 |
- package util
- import (
- "context"
- "net/http"
- "gitea.ckfah.com/cjjy/gocommon/pkg/net/httpclent"
- )
- /**
- 自定义一些Http-Client的一些通用的方法
- */
- const (
- GlobalUserId = "global-user-id"
- )
- var (
- CustomerHeaderHttpOption httpclent.Option = func(options *httpclent.Options) {
- options.HandlerRequestHeader = func(ctx context.Context, header http.Header) {
- // 首先使用默认的处理器处理header
- httpclent.DefaultHandlerRequestHandler(ctx, header)
- // 自定义的信息从ctx中拿到
- userId, _ := ctx.Value(GlobalUserId).(string)
- header.Add(GlobalUserId, userId)
- }
- }
- )
- /**
- 自定义添加校验http响应异常的方法,这个方法只需要全局调用一次,介意写在main函数初始化上,默认已经支持有 error.message,code.data.message格式的异常
- */
- func AddVialidator() {
- httpclent.AddValidatorResponseBody("data.message", func(body []byte) (err error) {
- //抓取到error
- return nil
- })
- }
|