diff --git a/pkg/httpclient/request.go b/pkg/httpclient/request.go index e16f0e0..095bee9 100644 --- a/pkg/httpclient/request.go +++ b/pkg/httpclient/request.go @@ -36,6 +36,9 @@ type Request struct { func (c *Request) Header(key, value string) *Request { c.mutex.Lock() defer c.mutex.Unlock() + if c.header == nil { + c.header = map[string][]string{} + } c.header.Add(key, value) if strings.ToLower(key) == "content-type" { c.contentType = value @@ -66,11 +69,19 @@ func (c *Request) GET(url string) *Request { func (c *Request) ContentType(value string) *Request { c.mutex.Lock() defer c.mutex.Unlock() + if c.header == nil { + c.header = map[string][]string{} + } c.header.Set("Content-Type", value) c.contentType = value return c } +func (c *Request) WithContentTypeAsJson() *Request { + c.ContentType("application/json") + return c +} + // WithHost 添加请求头host func (c *Request) WithHost(host string) *Request { c.host = host