go请求封装,写在commen哦

post请求

func RpcPostJson(postUrl, paramJson string) (string, interface{}, string) {
    log.Info("RpcPostJson请求开始 :", paramJson)
    req := bytes.NewBuffer([]byte(paramJson))
    var codeStr = util.ResultFail
    //放款通知接口
    if response, err := http.Post(postUrl, JsonHeader, req); err != nil {
        log.Error("RpcPostJson: err: ", err)
    } else {
        if resStr, err := ioutil.ReadAll(response.Body); err == nil {
            log.Info("routers: RpcPostJson: resStr: ", string(resStr))
            dat := make(map[string]interface{})
            if err = json.Unmarshal(resStr, &dat); err == nil {
                value := dat["code"]
                switch value.(type) {
                case string:
                    codeStr = value.(string)
                case int64:
                    codeStr = util.Int64ToString(value.(int64))
                case float64:
                    codeStr = util.FloatToString(value.(float64))
                }

                if codeStr == util.ResultSuccess {
                    if dat["data"] != nil {
                        return codeStr, dat["data"], string(resStr)
                    } else if dat["result"] != nil {
                        return codeStr, dat["result"], string(resStr)
                    }
                } else {
                    return codeStr, dat["message"], string(resStr)

                }
                log.Error("routers: RpcPostJson err: ", err)
            }
            return codeStr, nil, string(resStr)
        }
    }
    return codeStr, nil, ""
}

get请求

func RpcGetJson(url, paramStr string) (string, interface{}, string) {
    log.Info("RpcGetJson请求开始 :", paramStr)
    var codeStr = util.ResultFail
    //放款通知接口
    if paramStr != "" {
        url += "?" + paramStr
    }
    if response, err := http.Get(url); err != nil {
        log.Error("RpcGetJson: err: ", err)
    } else {
        if resStr, err := ioutil.ReadAll(response.Body); err == nil {
            log.Info("routers: RpcGetJson: resStr: ", string(resStr))
            dat := make(map[string]interface{})
            if err = json.Unmarshal(resStr, &dat); err == nil {
                value := dat["code"]
                switch value.(type) {
                case string:
                    codeStr = value.(string)
                case int64:
                    codeStr = util.Int64ToString(value.(int64))
                case float64:
                    codeStr = util.FloatToString(value.(float64))
                }

                if codeStr == util.ResultSuccess {
                    if dat["data"] != nil {
                        return codeStr, dat["data"], string(resStr)
                    } else if dat["result"] != nil {
                        return codeStr, dat["result"], string(resStr)
                    }
                }
                log.Error("routers: RpcGetJson err: ", err)
            }
            return codeStr, nil, string(resStr)
        }
    }
    return codeStr, nil, ""
}

你可能感兴趣的:(go请求封装,写在commen哦)