func JsonDecoder(reader io.Reader, v interface{}, trace retrace.Tracer) error { if bytes, err := ioutil.ReadAll(reader); err != nil { return err } else if err = json.Unmarshal(bytes, &v); err != nil { return err } else { trace.Annotate(retrace.FromRequestData, "body", fmt.Sprintf("%s", bytes)) return nil } }
func (cd *ContentTypeDecoders) DecodeBody(req *rest.Request, trace retrace.Tracer) error { switch req.Raw.Method { case "GET", "DELETE", "HEAD": return nil } ctype := req.Raw.Header["Content-Type"] decoder := cd.Get(ctype) //new empty instance body := req.Definition.RequestBody() var bts []byte var err error if bts, err = req.Bytes(); err != nil { return err } if body != nil { if isBytes(reflect.TypeOf(body)) { //if body type is castable to []byte, then we don't encode, just set directly req.Body = bts return nil } else if decoder != nil { decoder.Decode(bytes.NewReader(bts), &body, trace) req.Body = body } else if containsType(ctype, "application/x-www-form-urlencoded") { req.Raw.ParseForm() if err := util.MapHeaderToStruct(req.Raw.Form, &body); err != nil { return err } trace.Annotate(retrace.FromRequestData, "body", fmt.Sprintf("%+v", body)) req.Body = body } else { if err = json.Unmarshal(bts, &body); err != nil { return err } trace.Annotate(retrace.FromRequestData, "body", fmt.Sprintf("%s", bts)) req.Body = body } } return nil }