Skip to content

Commit 013d0f9

Browse files
committed
Remove UnmarshalParams, it was just a temporary fix for accepting json data
1 parent 66cf9c5 commit 013d0f9

File tree

2 files changed

+17
-29
lines changed

2 files changed

+17
-29
lines changed

request.go

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package web
22

33
import (
4+
"bytes"
45
"fmt"
56
"http"
67
"io"
78
"io/ioutil"
8-
"json"
99
"mime"
1010
"mime/multipart"
1111
"net"
@@ -164,12 +164,12 @@ func parseForm(m map[string][]string, query string) (err os.Error) {
164164
// ParseForm parses the request body as a form for POST requests, or the raw query for GET requests.
165165
// It is idempotent.
166166
func (r *Request) parseParams() (err os.Error) {
167+
167168
if r.Params != nil {
168169
return
169170
}
170171
r.FullParams = make(map[string][]string)
171172
queryParams := r.URL.RawQuery
172-
var bodyParams string
173173
switch r.Method {
174174
case "POST":
175175
if r.Body == nil {
@@ -179,21 +179,11 @@ func (r *Request) parseParams() (err os.Error) {
179179
ct := r.Headers.Get("Content-Type")
180180
switch strings.SplitN(ct, ";", 2)[0] {
181181
case "text/plain", "application/x-www-form-urlencoded", "":
182-
var b []byte
183-
if b, err = ioutil.ReadAll(r.Body); err != nil {
184-
return err
185-
}
186-
bodyParams = string(b)
187-
case "application/json":
188-
//if we get JSON, do the best we can to convert it to a map[string]string
189-
//we make the body available as r.ParamData
190182
var b []byte
191183
if b, err = ioutil.ReadAll(r.Body); err != nil {
192184
return err
193185
}
194186
r.ParamData = b
195-
r.Params = map[string]string{}
196-
json.Unmarshal(b, r.Params)
197187
case "multipart/form-data":
198188
_, params := mime.ParseMediaType(ct)
199189
boundary, ok := params["boundary"]
@@ -246,11 +236,12 @@ func (r *Request) parseParams() (err os.Error) {
246236
}
247237
}
248238

249-
if bodyParams != "" {
250-
err = parseForm(r.FullParams, bodyParams)
239+
if len(r.ParamData) > 0 {
240+
err = parseForm(r.FullParams, string(r.ParamData))
251241
if err != nil {
252242
return err
253243
}
244+
r.Body = bytes.NewBuffer(r.ParamData)
254245
}
255246

256247
r.Params = flattenParams(r.FullParams)
@@ -349,16 +340,3 @@ func (r *Request) writeToContainer(val reflect.Value) os.Error {
349340
}
350341
return nil
351342
}
352-
353-
func (r *Request) UnmarshalParams(val interface{}) os.Error {
354-
if strings.HasPrefix(r.Headers.Get("Content-Type"), "application/json") {
355-
return json.Unmarshal(r.ParamData, val)
356-
} else {
357-
err := r.writeToContainer(reflect.ValueOf(val))
358-
if err != nil {
359-
return err
360-
}
361-
}
362-
363-
return nil
364-
}

web_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ func init() {
171171
return string(data)
172172
})
173173

174+
Post("/parsejson", func(ctx *Context) string {
175+
var tmp = struct{ A string; B string }{}
176+
json.NewDecoder(ctx.Request.Body).Decode(&tmp)
177+
return tmp.A + " " + tmp.B
178+
})
179+
174180
//s := &StructHandler{"a"}
175181
//Get("/methodhandler", MethodHandler(s, "method"))
176182
//Get("/methodhandler2", MethodHandler(s, "method2"))
@@ -204,6 +210,7 @@ var tests = []Test{
204210
//{"GET", "/methodhandler", "", 200, `a`},
205211
//{"GET", "/methodhandler2?b=b", "", 200, `ab`},
206212
//{"GET", "/methodhandler3/b", "", 200, `ab`},
213+
{"POST", "/parsejson", `{"a":"hello", "b":"world"}`, 200, "hello world"},
207214
}
208215

209216
func buildTestRequest(method string, path string, body string, headers map[string][]string, cookies []*http.Cookie) *Request {
@@ -213,15 +220,17 @@ func buildTestRequest(method string, path string, body string, headers map[strin
213220
url_, _ := url.Parse(rawurl)
214221

215222
proto := "HTTP/1.1"
216-
useragent := "web.go test framework"
223+
useragent := "web.go test"
217224

218225
if headers == nil {
219226
headers = map[string][]string{}
220227
}
221228

222229
if method == "POST" {
223230
headers["Content-Length"] = []string{fmt.Sprintf("%d", len(body))}
224-
headers["Content-Type"] = []string{"text/plain"}
231+
if headers["Content-Type"] == nil {
232+
headers["Content-Type"] = []string{"text/plain"}
233+
}
225234
}
226235

227236
req := Request{Method: method,
@@ -663,6 +672,7 @@ func TestSecureCookieFcgi(t *testing.T) {
663672
}
664673
}
665674

675+
666676
//Disabled until issue 1375 is fixed
667677
/*
668678
func TestCloseServer(t *testing.T) {

0 commit comments

Comments
 (0)