Skip to content

Commit

Permalink
httpjson input set Content-Type correctly (#24336)
Browse files Browse the repository at this point in the history
Closes #24331
  • Loading branch information
leehinman authored Mar 8, 2021
1 parent 1fd8c4a commit 4f5c282
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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*

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"))
}
}

0 comments on commit 4f5c282

Please sign in to comment.