MCPcopy Create free account
hub / github.com/deepflowio/deepflow / parseResponse

Function parseResponse

cli/ctl/common/http_util.go:100–129  ·  view source on GitHub ↗
(req *http.Request, cfg *HTTPConf)

Source from the content-addressed store, hash-verified

98}
99
100func parseResponse(req *http.Request, cfg *HTTPConf) (*simplejson.Json, error) {
101 errResponse, _ := simplejson.NewJson([]byte("{}"))
102 // TODO: 通过配置文件获取API超时时间
103 client := &http.Client{Timeout: cfg.Timeout}
104 resp, err := client.Do(req)
105 if err != nil {
106 return errResponse, errors.New(fmt.Sprintf("curl (%s) failed, (%v)", req.URL, err))
107 }
108
109 defer resp.Body.Close()
110 respBytes, err := io.ReadAll(resp.Body)
111 if err != nil {
112 return errResponse, errors.New(fmt.Sprintf("read (%s) body failed, (%v)", req.URL, err))
113 }
114 if resp.StatusCode != http.StatusOK {
115 return errResponse, errors.New(fmt.Sprintf("curl (%s) failed, (%v)", req.URL, string(respBytes)))
116 }
117
118 response, err := simplejson.NewJson(respBytes)
119 if err != nil {
120 return errResponse, errors.New(fmt.Sprintf("parse (%s) body failed, (%v)", req.URL, err))
121 }
122
123 optStatus := response.Get("OPT_STATUS").MustString()
124 if optStatus != "" && optStatus != SUCCESS {
125 description := response.Get("DESCRIPTION").MustString()
126 return response, errors.New(fmt.Sprintf("curl (%s) failed, (%v)", req.URL, description))
127 }
128 return response, nil
129}
130
131func CURLPostFormData(url, contentType string, body *bytes.Buffer, opts ...HTTPOption) (*simplejson.Json, error) {
132 cfg := &HTTPConf{}

Callers 2

CURLPerformFunction · 0.85
CURLPostFormDataFunction · 0.85

Calls 3

NewMethod · 0.80
CloseMethod · 0.65
GetMethod · 0.65

Tested by

no test coverage detected