Skip to content

Commit 6cbe91f

Browse files
authored
Compatibility header should not set content-type when body is nil (#406) (#407)
* Only send content-type compatibility header if body is present * Add test to check for content-type in case of an empty body
1 parent f23a373 commit 6cbe91f

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

elasticsearch.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ const (
4848
// HeaderClientMeta Key for the HTTP Header related to telemetry data sent with
4949
// each request to Elasticsearch.
5050
HeaderClientMeta = "x-elastic-client-meta"
51+
52+
compatibilityHeader = "application/vnd.elasticsearch+json;compatible-with=8"
5153
)
5254

5355
var (
@@ -240,8 +242,10 @@ func NewClient(cfg Config) (*Client, error) {
240242
func (c *Client) Perform(req *http.Request) (*http.Response, error) {
241243
// Compatibility Header
242244
if c.compatibilityHeader {
243-
req.Header.Set("Content-Type", "application/vnd.elasticsearch+json;compatible-with=8")
244-
req.Header.Set("Accept", "application/vnd.elasticsearch+json;compatible-with=8")
245+
if req.Body != nil {
246+
req.Header.Set("Content-Type", compatibilityHeader)
247+
}
248+
req.Header.Set("Accept", compatibilityHeader)
245249
}
246250

247251
if !c.disableMetaHeader {

elasticsearch_internal_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,10 @@ func TestCompatibilityHeader(t *testing.T) {
630630
if !reflect.DeepEqual(req.Header["Content-Type"], test.expectsHeader) {
631631
t.Errorf("Compatibility header with Body enabled, not in request headers, got: %s, want: %s", req.Header["Content-Type"], test.expectsHeader)
632632
}
633+
} else {
634+
if reflect.DeepEqual(req.Header["Content-Type"], test.expectsHeader) {
635+
t.Errorf("Compatibility header if Content-Type shouldn't be set with an empty body")
636+
}
633637
}
634638
}
635639

0 commit comments

Comments
 (0)