Skip to content

Commit 286db03

Browse files
author
Dean Karn
committed
DecodeJSON encoing aware
Added encoding awareness to the DecodeJSON helper function.
1 parent dd99f50 commit 286db03

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package pure
22
============
3-
<img align="right" src="https://raw.githubusercontent.com/go-playground/pure/master/logo.png">![Project status](https://img.shields.io/badge/version-4.1.1-green.svg)
3+
<img align="right" src="https://raw.githubusercontent.com/go-playground/pure/master/logo.png">![Project status](https://img.shields.io/badge/version-4.2.0-green.svg)
44
[![Build Status](https://travis-ci.org/go-playground/pure.svg?branch=master)](https://travis-ci.org/go-playground/pure)
55
[![Coverage Status](https://coveralls.io/repos/github/go-playground/pure/badge.svg?branch=master)](https://coveralls.io/github/go-playground/pure?branch=master)
66
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/pure)](https://goreportcard.com/report/github.com/go-playground/pure)

_examples/basic/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
func main() {
1111

1212
p := pure.New()
13-
p.Use(mw.LoggingAndRecovery(true))
13+
p.Use(mw.LoggingAndRecovery(false))
1414

1515
p.Get("/", helloWorld)
1616

helpers.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package pure
22

33
import (
4+
"compress/gzip"
45
"encoding/json"
56
"encoding/xml"
67
"io"
@@ -333,13 +334,18 @@ func DecodeMultipartForm(r *http.Request, includeQueryParams bool, maxMemory int
333334
// included eg. route /user/:id?test=true both 'id' and 'test' are treated as query params and
334335
// added to parsed JSON; in short SEO query params are treated just like normal query params.
335336
func DecodeJSON(r *http.Request, includeQueryParams bool, maxMemory int64, v interface{}) (err error) {
337+
var body io.Reader = r.Body
336338

337-
err = json.NewDecoder(io.LimitReader(r.Body, maxMemory)).Decode(v)
338-
339+
if encoding := r.Header.Get(ContentEncoding); encoding == Gzip {
340+
body, err = gzip.NewReader(r.Body)
341+
if err != nil {
342+
return err
343+
}
344+
}
345+
err = json.NewDecoder(io.LimitReader(body, maxMemory)).Decode(v)
339346
if includeQueryParams && err == nil {
340347
err = DecodeQueryParams(r, includeQueryParams, v)
341348
}
342-
343349
return
344350
}
345351

helpers_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,30 @@ func TestDecode(t *testing.T) {
219219
Equal(t, test.Posted, "value")
220220
Equal(t, test.MultiPartPosted, "value")
221221

222+
var buff bytes.Buffer
223+
gzw := gzip.NewWriter(&buff)
224+
defer func() {
225+
_ = gzw.Close()
226+
}()
227+
_, err = gzw.Write([]byte(jsonBody))
228+
Equal(t, err, nil)
229+
230+
err = gzw.Close()
231+
Equal(t, err, nil)
232+
233+
test = new(TestStruct)
234+
r, _ = http.NewRequest(http.MethodPost, "/decode/13?id=14", &buff)
235+
r.Header.Set(ContentType, ApplicationJSON)
236+
r.Header.Set(ContentEncoding, Gzip)
237+
w = httptest.NewRecorder()
238+
239+
hf.ServeHTTP(w, r)
240+
241+
Equal(t, w.Code, http.StatusOK)
242+
Equal(t, test.ID, 14)
243+
Equal(t, test.Posted, "value")
244+
Equal(t, test.MultiPartPosted, "value")
245+
222246
test = new(TestStruct)
223247
r, _ = http.NewRequest(http.MethodPost, "/decode-noquery/13?id=14", strings.NewReader(jsonBody))
224248
r.Header.Set(ContentType, ApplicationJSON)

0 commit comments

Comments
 (0)