适配支持从Header或者Body解析请求结果

This commit is contained in:
白茶清欢 2025-05-29 23:11:56 +08:00
parent 5ddab034f3
commit 42b9f26a9a
2 changed files with 15 additions and 2 deletions

View File

@ -30,6 +30,7 @@ type Request struct {
Method string `json:"method"` // 请求方法
DataField string `json:"data_field"` // 数据字段
CodeField string `json:"code_field"` // 业务状态码字段
CodeLocation string `json:"code_location"` // 业务状态码位置
MessageField string `json:"message_field"` // code描述字段
DataReceiver any `json:"-"` // 响应data部分数据解析
SuccessHttpCodeList []int `json:"success_http_code_list"` // 哪些http状态码视为成功, 不配置, 默认2xx

View File

@ -94,8 +94,20 @@ func (r *Response) fillResponseBody(reqCfg *define.Request, response *define.Res
return errors.New("response body Marshal error :" + err.Error())
}
response.Data = string(jsonByte)
response.Code = gjson.Get(response.Data, reqCfg.CodeField).String()
response.Message = gjson.Get(response.Data, reqCfg.MessageField).String()
if strings.ToLower(reqCfg.CodeLocation) == "header" {
if reqCfg.CodeField == "code" {
response.Code = fmt.Sprintf("%v", response.HttpCode)
response.Message = response.RestyResponse.Status()
} else {
response.Code = response.RestyResponse.Header().Get(reqCfg.CodeField)
response.Message = response.RestyResponse.Header().Get(reqCfg.MessageField)
}
} else {
// 统一认为Body
response.Code = gjson.Get(response.Data, reqCfg.CodeField).String()
response.Message = gjson.Get(response.Data, reqCfg.MessageField).String()
}
businessData := gjson.Get(response.Data, reqCfg.DataField)
if businessData.Value() == nil {
// data为空指针, 归一化成空对象