Skip to content

Commit 8614445

Browse files
authored
Fix parsing of JSON with a UTF8 BOM in httpjson (#3267)
1 parent f23d1eb commit 8614445

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

plugins/inputs/httpjson/httpjson.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package httpjson
22

33
import (
4+
"bytes"
45
"fmt"
56
"io/ioutil"
67
"net/http"
@@ -15,6 +16,10 @@ import (
1516
"github.com/influxdata/telegraf/plugins/parsers"
1617
)
1718

19+
var (
20+
utf8BOM = []byte("\xef\xbb\xbf")
21+
)
22+
1823
// HttpJson struct
1924
type HttpJson struct {
2025
Name string
@@ -170,7 +175,6 @@ func (h *HttpJson) gatherServer(
170175
serverURL string,
171176
) error {
172177
resp, responseTime, err := h.sendRequest(serverURL)
173-
174178
if err != nil {
175179
return err
176180
}
@@ -266,6 +270,7 @@ func (h *HttpJson) sendRequest(serverURL string) (string, float64, error) {
266270
if err != nil {
267271
return string(body), responseTime, err
268272
}
273+
body = bytes.TrimPrefix(body, utf8BOM)
269274

270275
// Process response
271276
if resp.StatusCode != http.StatusOK {

plugins/inputs/httpjson/httpjson_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -560,3 +560,18 @@ func TestHttpJsonArray200Tags(t *testing.T) {
560560
}
561561
}
562562
}
563+
564+
var jsonBOM = []byte("\xef\xbb\xbf[{\"value\":17}]")
565+
566+
// TestHttpJsonBOM tests that UTF-8 JSON with a BOM can be parsed
567+
func TestHttpJsonBOM(t *testing.T) {
568+
httpjson := genMockHttpJson(string(jsonBOM), 200)
569+
570+
for _, service := range httpjson {
571+
if service.Name == "other_webapp" {
572+
var acc testutil.Accumulator
573+
err := acc.GatherError(service.Gather)
574+
require.NoError(t, err)
575+
}
576+
}
577+
}

0 commit comments

Comments
 (0)