Skip to content

Commit 52f3f39

Browse files
authored
Merge pull request #1079 from go-resty/fix-cnt-type-hdr-on-emptybody
fix: content type request header on http.NoBody type #1074
2 parents d4c3576 + 16e2701 commit 52f3f39

5 files changed

Lines changed: 25 additions & 4 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
<p align="center"><a href="#features">Features</a> section describes in detail about Resty capabilities</p>
55
</p>
66
<p align="center">
7-
<p align="center"><a href="https://github.com/go-resty/resty/actions/workflows/ci.yml?query=branch%3Av2"><img src="https://github.com/go-resty/resty/actions/workflows/ci.yml/badge.svg?branch=v2" alt="Build Status"></a> <a href="https://app.codecov.io/gh/go-resty/resty/tree/v2"><img src="https://codecov.io/gh/go-resty/resty/branch/v2/graph/badge.svg" alt="Code Coverage"></a> <a href="https://goreportcard.com/report/go-resty/resty"><img src="https://goreportcard.com/badge/go-resty/resty" alt="Go Report Card"></a> <a href="https://github.com/go-resty/resty/releases/latest"><img src="https://img.shields.io/badge/version-2.17.0-blue.svg" alt="Release Version"></a> <a href="https://pkg.go.dev/github.com/go-resty/resty/v2"><img src="https://pkg.go.dev/badge/github.com/go-resty/resty" alt="GoDoc"></a> <a href="LICENSE"><img src="https://img.shields.io/github/license/go-resty/resty.svg" alt="License"></a> <a href="https://github.com/avelino/awesome-go"><img src="https://awesome.re/mentioned-badge.svg" alt="Mentioned in Awesome Go"></a></p>
7+
<p align="center"><a href="https://github.com/go-resty/resty/actions/workflows/ci.yml?query=branch%3Av2"><img src="https://github.com/go-resty/resty/actions/workflows/ci.yml/badge.svg?branch=v2" alt="Build Status"></a> <a href="https://app.codecov.io/gh/go-resty/resty/tree/v2"><img src="https://codecov.io/gh/go-resty/resty/branch/v2/graph/badge.svg" alt="Code Coverage"></a> <a href="https://goreportcard.com/report/go-resty/resty"><img src="https://goreportcard.com/badge/go-resty/resty" alt="Go Report Card"></a> <a href="https://github.com/go-resty/resty/releases/latest"><img src="https://img.shields.io/badge/version-2.17.1-blue.svg" alt="Release Version"></a> <a href="https://pkg.go.dev/github.com/go-resty/resty/v2"><img src="https://pkg.go.dev/badge/github.com/go-resty/resty" alt="GoDoc"></a> <a href="LICENSE"><img src="https://img.shields.io/github/license/go-resty/resty.svg" alt="License"></a> <a href="https://github.com/avelino/awesome-go"><img src="https://awesome.re/mentioned-badge.svg" alt="Mentioned in Awesome Go"></a></p>
88
</p>
99

1010
## News
1111

12-
* v2.17.0 [released](https://github.com/go-resty/resty/releases/tag/v2.17.0) and tagged on Nov 22, 2025.
12+
* v2.17.1 [released](https://github.com/go-resty/resty/releases/tag/v2.17.1) and tagged on Dec 15, 2025.
1313
* v2.0.0 [released](https://github.com/go-resty/resty/releases/tag/v2.0.0) and tagged on Jul 16, 2019.
1414
* v1.12.0 [released](https://github.com/go-resty/resty/releases/tag/v1.12.0) and tagged on Feb 27, 2019.
1515
* v1.0 released and tagged on Sep 25, 2017. - Resty's first version was released on Sep 15, 2015 then it grew gradually as a very handy and helpful library. Its been a two years since first release. I'm very thankful to Resty users and its [contributors](https://github.com/go-resty/resty/graphs/contributors).

middleware.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,9 @@ func handleFormData(c *Client, r *Request) {
501501
}
502502

503503
func handleContentType(c *Client, r *Request) {
504+
if r.Body == http.NoBody {
505+
return
506+
}
504507
contentType := r.Header.Get(hdrContentTypeKey)
505508
if IsStringEmpty(contentType) {
506509
contentType = DetectContentType(r.Body)

request.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,8 @@ type SRVRecord struct {
10951095

10961096
func (r *Request) fmtBodyString(sl int64) (body string) {
10971097
body = "***** NO CONTENT *****"
1098-
if !isPayloadSupported(r.Method, r.client.AllowGetMethodPayload) {
1098+
if !isPayloadSupported(r.Method, r.client.AllowGetMethodPayload) ||
1099+
r.Body == http.NoBody {
10991100
return
11001101
}
11011102

request_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2397,3 +2397,20 @@ func TestSetContentLengthTrueWithNilBody(t *testing.T) {
23972397
// when body is nil and SetContentLength is true expect Content-Length == "0"
23982398
assertEqual(t, "0", resp.Header().Get("Request-Content-Length"))
23992399
}
2400+
2401+
func TestGH1074ContentTypeHdrIssue(t *testing.T) {
2402+
ts := createTestServer(func(w http.ResponseWriter, r *http.Request) {
2403+
t.Logf("Method: %v", r.Method)
2404+
t.Logf("Path: %v", r.URL.Path)
2405+
t.Logf("Headers: %v", r.Header)
2406+
2407+
assertEqual(t, "", r.Header.Get(hdrContentTypeKey))
2408+
2409+
w.WriteHeader(http.StatusOK)
2410+
})
2411+
defer ts.Close()
2412+
2413+
c := dc()
2414+
_, err := c.R().Delete(ts.URL)
2415+
assertNil(t, err)
2416+
}

resty.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
// Version # of resty
17-
const Version = "2.16.5"
17+
const Version = "2.17.1"
1818

1919
// New method creates a new Resty client.
2020
func New() *Client {

0 commit comments

Comments
 (0)