readRequestJSON expects req to have a JSON content type with a body that contains a JSON-encoded value complying with the underlying type of target. It populates target, or returns an error.
(req *http.Request, target any)
| 15 | // contains a JSON-encoded value complying with the underlying type of target. |
| 16 | // It populates target, or returns an error. |
| 17 | func readRequestJSON(req *http.Request, target any) error { |
| 18 | contentType := req.Header.Get("Content-Type") |
| 19 | mediaType, _, err := mime.ParseMediaType(contentType) |
| 20 | if err != nil { |
| 21 | return err |
| 22 | } |
| 23 | if mediaType != "application/json" { |
| 24 | return fmt.Errorf("expect application/json Content-Type, got %s", mediaType) |
| 25 | } |
| 26 | |
| 27 | dec := json.NewDecoder(req.Body) |
| 28 | dec.DisallowUnknownFields() |
| 29 | return dec.Decode(target) |
| 30 | } |
| 31 | |
| 32 | // renderJSON renders 'v' as JSON and writes it as a response into w. |
| 33 | func renderJSON(w http.ResponseWriter, v any) { |
no outgoing calls
no test coverage detected
searching dependent graphs…