gitee.com/h79/goutils@v1.22.10/loader/http.go (about) 1 package loader 2 3 import ( 4 commonhttp "gitee.com/h79/goutils/common/http" 5 "net/http" 6 ) 7 8 type Http struct { 9 Loader 10 headFunc commonhttp.HeaderFunc 11 url string 12 da interface{} 13 } 14 15 func NewHttp(url string, data interface{}, headFunc commonhttp.HeaderFunc) *Loader { 16 hp := &Http{ 17 Loader: CreateLoader(), 18 headFunc: headFunc, 19 url: url, 20 da: data, 21 } 22 return hp.WithLoadFunc(hp.load) 23 } 24 25 func (hp *Http) load() (interface{}, error) { 26 chp := commonhttp.Http{} 27 28 body, err := chp.Do("GET", hp.url, nil, func(h *http.Header) { 29 h.Set("Content-Type", "application/json;charset=utf-8") 30 if hp.headFunc != nil { 31 hp.headFunc(h) 32 } 33 }) 34 if err != nil { 35 return nil, err 36 } 37 if err = hp.unmarshal.Unmarshal(body, hp.da); err != nil { 38 return nil, err 39 } 40 return hp.da, err 41 }