Просмотр исходного кода

给PostBytes添加header参数

huangrf 5 лет назад
Родитель
Сommit
d36f6c91b4
1 измененных файлов с 10 добавлено и 3 удалено
  1. 10 3
      utils/http_util.go

+ 10 - 3
utils/http_util.go

@@ -26,7 +26,7 @@ func NewHttpUtil() *HttpUtil {
 }
 
 // 使用body post提交数据
-func (this *HttpUtil) PostBytes(url string, data []byte) ([]byte, error) {
+func (this *HttpUtil) PostBytes(url string, data []byte, headers map[string]string) ([]byte, error) {
 
 	body := bytes.NewReader(data)
 	request, err := http.NewRequest("POST", url, body)
@@ -34,8 +34,15 @@ func (this *HttpUtil) PostBytes(url string, data []byte) ([]byte, error) {
 		//log.Println("http.NewRequest,[err=%s][url=%s]", err, url)
 		return []byte(""), err
 	}
-	request.Header.Set("Connection", "Keep-Alive")
-	request.Header.Set("Content-Type", "application/json;charset=utf-8")
+
+	if headers == nil || len(headers) == 0 {
+		request.Header.Set("Connection", "Keep-Alive")
+		request.Header.Set("Content-Type", "application/json;charset=utf-8")
+	}else{
+		for k, v := range headers {
+			request.Header.Set(k, v)
+		}
+	}
 
 	return this.getResponse(request)
 }