(r *http.Request, destination interface{}, o *httpDecoderOptions)
| 535 | } |
| 536 | |
| 537 | func decodeJSON(r *http.Request, destination interface{}, o *httpDecoderOptions) error { |
| 538 | reader, err := requestBody(r, o) |
| 539 | if err != nil { |
| 540 | return err |
| 541 | } |
| 542 | |
| 543 | raw, err := io.ReadAll(reader) |
| 544 | if err != nil { |
| 545 | return errors.WithStack(herodot.ErrBadRequest.WithReasonf("Unable to read HTTP POST body: %s", err)) |
| 546 | } |
| 547 | |
| 548 | dc := json.NewDecoder(bytes.NewReader(raw)) |
| 549 | if err := dc.Decode(destination); err != nil { |
| 550 | return errors.WithStack(herodot.ErrBadRequest.WithReasonf("Unable to decode JSON payload: %s", err).WithDebugf("Received request body: %s", string(raw))) |
| 551 | } |
| 552 | |
| 553 | if err := validatePayload(r.Context(), raw, o); err != nil { |
| 554 | if o.expectJSONFlattened && strings.Contains(err.Error(), "json: unknown field") { |
| 555 | return decodeJSONForm(r, destination, o) |
| 556 | } |
| 557 | return err |
| 558 | } |
| 559 | |
| 560 | return nil |
| 561 | } |
no test coverage detected