Browse Source

添加JSON可以配置Header

yjp 2 năm trước cách đây
mục cha
commit
f2918aa6d6
1 tập tin đã thay đổi với 14 bổ sung8 xóa
  1. 14 8
      invoke/invoke.go

+ 14 - 8
invoke/invoke.go

@@ -7,10 +7,6 @@ import (
 	"time"
 )
 
-var (
-	contentTypeJSONHeader = map[string]string{"ContentType": "application/json"}
-)
-
 type API struct {
 	client  *resty.Client
 	baseUrl string
@@ -32,12 +28,22 @@ func DestroyAPI(api *API) {
 	api.client = nil
 }
 
-func (api *API) PostJSON(methodName string, data []byte) ([]byte, error) {
-	return api.Post(methodName, contentTypeJSONHeader, data)
+func (api *API) PostJSON(methodName string, data []byte, headers map[string]string) ([]byte, error) {
+	if headers == nil {
+		headers = make(map[string]string)
+	}
+
+	headers["Content-Type"] = "application/json"
+	return api.Post(methodName, headers, data)
 }
 
-func (api *API) PutJSON(methodName string, data []byte) ([]byte, error) {
-	return api.Put(methodName, contentTypeJSONHeader, data)
+func (api *API) PutJSON(methodName string, data []byte, headers map[string]string) ([]byte, error) {
+	if headers == nil {
+		headers = make(map[string]string)
+	}
+
+	headers["Content-Type"] = "application/json"
+	return api.Put(methodName, headers, data)
 }
 
 func (api *API) PostWithoutHeaders(methodName string, data []byte) ([]byte, error) {