Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Filebeat] httpjson input set Content-Type correctly #24336

Merged
merged 1 commit into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,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*

Expand Down
2 changes: 1 addition & 1 deletion x-pack/filebeat/docs/inputs/input-httpjson.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
6 changes: 6 additions & 0 deletions x-pack/filebeat/input/httpjson/internal/v2/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand All @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
}