Browse Source

update http request default

Ken 2 weeks ago
parent
commit
9a93b54c8e
1 changed files with 47 additions and 0 deletions
  1. 47 0
      request.go

+ 47 - 0
request.go

@@ -23,6 +23,17 @@ const (
 	// ReqContentTypeMultipart multipart/form-data
 	ReqContentTypeMultipart = `multipart/form-data`
 
+	// HTTP header Accept-Encoding
+	AcceptEncoding = `Accept-Encoding`
+	// HTTP header Content-Type
+	ContentType = `Content-Type`
+
+	// Default Accept-Encoding
+	DefaultAcceptEncoding = `gzip, deflate, br`
+
+	// ContentTypeJSON application/json
+	ContentTypeJSON = `application/json; charset=utf-8`
+
 	// RequestTimeOut http request timeout (second)
 	RequestTimeOut = 30
 )
@@ -54,6 +65,42 @@ func SetHTTPRequestTimeout(seconds int) {
 	reqTimeOut = time.Duration(seconds)
 }
 
+// HTTPHeader HTTP header
+type HTTPHeader struct {
+	header map[string]string
+}
+
+// DefaultTTPHeader default HTTP header
+var DefaultTTPHeader = NewDefaultHTTPHeader()
+
+// NewHTTPHeader new HTTPHeader
+func NewHTTPHeader() (h *HTTPHeader) {
+	return &HTTPHeader{header: map[string]string{}}
+}
+
+// NewDefaultHTTPHeader new HTTPHeader
+func NewDefaultHTTPHeader() (h *HTTPHeader) {
+	h = NewHTTPHeader()
+	//h.Add(ContentType, ContentTypeJSON)
+	h.Add(AcceptEncoding, DefaultAcceptEncoding)
+	return
+}
+
+// Headers return header
+func (h *HTTPHeader) Headers() map[string]string {
+	return h.header
+}
+
+// Add add new k-v
+func (h *HTTPHeader) Add(k, v string) {
+	h.header[k] = v
+}
+
+// Del del k
+func (h *HTTPHeader) Del(k string) {
+	delete(h.header, k)
+}
+
 // HTTPReqOption request option
 type HTTPReqOption struct {
 }