diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 884a20f5aa17..9a4eee666e1d 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -395,6 +395,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d - Zoom module pipeline failed to ingest some chat_channel events. {pull}23904[23904] - Fix Netflow module issue with missing `internal_networks` config parameter. {issue}24094[24094] {pull}24110[24110] - Fix aws/vpcflow generating errors for empty logs or unidentified formats. {pull}24167[24167] +- in httpjson input using encode_as "application/x-www-form-urlencoded" now sets Content-Type correctly {issue}24331[24331] {pull}24336[24336] *Heartbeat* diff --git a/x-pack/filebeat/docs/inputs/input-httpjson.asciidoc b/x-pack/filebeat/docs/inputs/input-httpjson.asciidoc index 56b022d3e9a0..be5763830ad8 100644 --- a/x-pack/filebeat/docs/inputs/input-httpjson.asciidoc +++ b/x-pack/filebeat/docs/inputs/input-httpjson.asciidoc @@ -349,7 +349,7 @@ HTTP method to use when making requests. `GET` or `POST` are the options. Defaul [float] ==== `request.encode_as` -ContentType used for encoding the request body. If set it will force the encoding in the specified format regardless of the `Content-Type` header value, otherwise it will honor it if possible or fallback to `application/json`. By default the requests are sent with `Content-Type: application/json`. Supported values: `application/json` and `application\x-www-form-urlencoded`. `application/x-www-form-encoded` will url encode the `url.params` and set them as the body. It is not set by default. +ContentType used for encoding the request body. If set it will force the encoding in the specified format regardless of the `Content-Type` header value, otherwise it will honor it if possible or fallback to `application/json`. By default the requests are sent with `Content-Type: application/json`. Supported values: `application/json` and `application/x-www-form-urlencoded`. `application/x-www-form-urlencoded` will url encode the `url.params` and set them as the body. It is not set by default. [float] ==== `request.body` diff --git a/x-pack/filebeat/input/httpjson/internal/v2/encoding.go b/x-pack/filebeat/input/httpjson/internal/v2/encoding.go index f57dec837ee1..30768c3e4168 100644 --- a/x-pack/filebeat/input/httpjson/internal/v2/encoding.go +++ b/x-pack/filebeat/input/httpjson/internal/v2/encoding.go @@ -91,6 +91,9 @@ func encodeAsJSON(trReq transformable) ([]byte, error) { if len(trReq.body()) == 0 { return nil, nil } + header := trReq.header() + header.Set("Content-Type", "application/json") + trReq.setHeader(header) return json.Marshal(trReq.body()) } @@ -103,6 +106,9 @@ func encodeAsForm(trReq transformable) ([]byte, error) { body := []byte(url.RawQuery) url.RawQuery = "" trReq.setURL(url) + header := trReq.header() + header.Set("Content-Type", "application/x-www-form-urlencoded") + trReq.setHeader(header) return body, nil } diff --git a/x-pack/filebeat/input/httpjson/internal/v2/encoding_test.go b/x-pack/filebeat/input/httpjson/internal/v2/encoding_test.go index 4e3ba39f56bd..2cc50a59c7b9 100644 --- a/x-pack/filebeat/input/httpjson/internal/v2/encoding_test.go +++ b/x-pack/filebeat/input/httpjson/internal/v2/encoding_test.go @@ -61,5 +61,6 @@ func TestEncodeAsForm(t *testing.T) { trReq.setURL(*u) res, err := encodeAsForm(trReq) assert.Equal(t, test.body, string(res)) + assert.Equal(t, "application/x-www-form-urlencoded", trReq.header().Get("Content-Type")) } }