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

add PostJSONWithRespContentType

This commit is contained in:
jefferwang(王俊锋)
2019-02-19 15:11:45 +08:00
parent eda287070d
commit 61476d351d
2 changed files with 27 additions and 1 deletions

View File

@@ -5,7 +5,7 @@ import (
"fmt"
"strings"
"github.com/silenceper/wechat/util"
"github.com/JefferyWang/wechat/util"
)
const (

View File

@@ -50,6 +50,32 @@ func PostJSON(uri string, obj interface{}) ([]byte, error) {
return ioutil.ReadAll(response.Body)
}
// PostJSONWithRespContentType post json数据请求且返回数据类型
func PostJSONWithRespContentType(uri string, obj interface{}) ([]byte, string, error) {
jsonData, err := json.Marshal(obj)
if err != nil {
return nil, "", err
}
jsonData = bytes.Replace(jsonData, []byte("\\u003c"), []byte("<"), -1)
jsonData = bytes.Replace(jsonData, []byte("\\u003e"), []byte(">"), -1)
jsonData = bytes.Replace(jsonData, []byte("\\u0026"), []byte("&"), -1)
body := bytes.NewBuffer(jsonData)
response, err := http.Post(uri, "application/json;charset=utf-8", body)
if err != nil {
return nil, "", err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, "", fmt.Errorf("http get error : uri=%v , statusCode=%v", uri, response.StatusCode)
}
responseData, err := ioutil.ReadAll(response.Body)
contentType := response.Header.Get("Content-Type")
return responseData, contentType, err
}
//PostFile 上传文件
func PostFile(fieldname, filename, uri string) ([]byte, error) {
fields := []MultipartFormField{