收藏
回答

获取临时素材API接口返回body是乱码?

// 构建请求URL
    url := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s",
        req.AccessToken, req.MediaId)


    // 创建GET请求
    httpReq, err := http.NewRequest("GET", url, nil)
    if err != nil {
        tlog.Error("创建请求失败: %v\n", err)
        return nil, bizerr.New(vi_errcode.VI_CODE_COMM_ServerErr, fmt.Sprintf("创建请求失败: %v", err))
    }


    // 发送请求
    client := &http.Client{
        Timeout: config.Conf.Global.Timeout.Duration(),
    }
    transport := &http.Transport{
        ForceAttemptHTTP2: true,
        DisableKeepAlives: true,
        Proxy:             rpc.HTTPProxyJudgeInternal(config.Conf.Global.HTTPProxy, 3, 200*time.Millisecond),
    }
    client.Transport = transport
    resp, err := client.Do(httpReq)
    if err != nil {
        tlog.Error("发送请求失败: %v\n", err)
        return nil, bizerr.New(vi_errcode.VI_CODE_COMM_ServerErr, fmt.Sprintf("发送请求失败: %v", err))
    }
    defer resp.Body.Close()


    // 读取响应体内容
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        tlog.Error("读取响应体失败: %v\n", err)
        return nil, bizerr.New(vi_errcode.VI_CODE_COMM_ServerErr, fmt.Sprintf("读取响应体失败: %v", err))
    }


    // 只打印响应体内容
    tlog.Debug("resp:%s", string(body))


回答关注问题邀请回答
收藏

1 个回答

登录 后发表内容