1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-04 12:52:27 +08:00

cache增加带Context版本,开放平台相关接口支持Context版本 (#653)

This commit is contained in:
okhowang
2023-04-03 20:32:44 +08:00
committed by GitHub
parent 01784c2a4a
commit 07b7dc40fc
7 changed files with 178 additions and 43 deletions

View File

@@ -69,8 +69,8 @@ func HTTPPostContext(ctx context.Context, uri string, data []byte, header map[st
return io.ReadAll(response.Body)
}
// PostJSON post json 数据请求
func PostJSON(uri string, obj interface{}) ([]byte, error) {
// PostJSONContext post json 数据请求
func PostJSONContext(ctx context.Context, uri string, obj interface{}) ([]byte, error) {
jsonBuf := new(bytes.Buffer)
enc := json.NewEncoder(jsonBuf)
enc.SetEscapeHTML(false)
@@ -78,7 +78,12 @@ func PostJSON(uri string, obj interface{}) ([]byte, error) {
if err != nil {
return nil, err
}
response, err := http.Post(uri, "application/json;charset=utf-8", jsonBuf)
req, err := http.NewRequestWithContext(ctx, "POST", uri, jsonBuf)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json;charset=utf-8")
response, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
@@ -90,6 +95,11 @@ func PostJSON(uri string, obj interface{}) ([]byte, error) {
return io.ReadAll(response.Body)
}
// PostJSON post json 数据请求
func PostJSON(uri string, obj interface{}) ([]byte, error) {
return PostJSONContext(context.Background(), uri, obj)
}
// PostJSONWithRespContentType post json数据请求且返回数据类型
func PostJSONWithRespContentType(uri string, obj interface{}) ([]byte, string, error) {
jsonBuf := new(bytes.Buffer)