Skip to content

Commit

Permalink
feat: add way to set empty header on request #496
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Oct 1, 2024
1 parent 8a65c82 commit 634b218
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 6 additions & 3 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,18 @@ func parseRequestHeader(c *Client, r *Request) error {
r.Header[k] = v[:]
}

if isStringEmpty(r.Header.Get(hdrUserAgentKey)) {
if !r.isHeaderExists(hdrUserAgentKey) {
r.Header.Set(hdrUserAgentKey, hdrUserAgentValue)
}

if isStringEmpty(r.Header.Get(hdrAcceptKey)) {
if !r.isHeaderExists(hdrAcceptKey) {
ct := r.Header.Get(hdrContentTypeKey)
if isJSONContentType(ct) || isXMLContentType(ct) {
r.Header.Set(hdrAcceptKey, ct)
}
}

if isStringEmpty(r.Header.Get(hdrAcceptEncodingKey)) {
if !r.isHeaderExists(hdrAcceptEncodingKey) {
r.Header.Set(hdrAcceptEncodingKey, r.client.ContentDecompressorKeys())
}

Expand Down Expand Up @@ -498,7 +498,10 @@ func handleRequestBody(c *Client, r *Request) error {
contentType := r.Header.Get(hdrContentTypeKey)
if isStringEmpty(contentType) {
// it is highly recommended that the user provide a request content-type
// so that we can minimize memory allocation and compute.
contentType = detectContentType(r.Body)
}
if !r.isHeaderExists(hdrContentTypeKey) {
r.Header.Set(hdrContentTypeKey, contentType)
}

Expand Down
5 changes: 5 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,11 @@ func (r *Request) initClientTrace() {
}
}

func (r *Request) isHeaderExists(k string) bool {
_, f := r.Header[k]
return f
}

func jsonIndent(v []byte) []byte {
buf := acquireBuffer()
defer releaseBuffer(buf)
Expand Down

0 comments on commit 634b218

Please sign in to comment.