Unmarshal processes the request content and fills passed data struct with the correct jsonapi content. After un-marshaling the struct will be validated with specified go-validator struct tags. In case of an error, an jsonapi error message will be directly send to the client
(w http.ResponseWriter, r *http.Request, data interface{})
| 17 | // specified go-validator struct tags. |
| 18 | // In case of an error, an jsonapi error message will be directly send to the client |
| 19 | func Unmarshal(w http.ResponseWriter, r *http.Request, data interface{}) bool { |
| 20 | // don't leak , but error can't be handled |
| 21 | defer r.Body.Close() // nolint: errcheck |
| 22 | |
| 23 | // verify that the client accepts our response |
| 24 | // Note: logically this would be done before marshalling, |
| 25 | // to prevent stale backend/frontend state we respond before |
| 26 | // Additionally, marshal has no access to the request struct |
| 27 | accept := r.Header.Get("Accept") |
| 28 | if accept != JSONAPIContentType { |
| 29 | WriteError(w, http.StatusNotAcceptable, |
| 30 | fmt.Errorf("request needs to be send with %q header, containing value: %q", "Accept", JSONAPIContentType)) |
| 31 | return false |
| 32 | } |
| 33 | |
| 34 | // if the client didn't send a content type, don't verify |
| 35 | contentType := r.Header.Get("Content-Type") |
| 36 | if contentType != JSONAPIContentType { |
| 37 | WriteError(w, http.StatusUnsupportedMediaType, |
| 38 | fmt.Errorf("request needs to be send with %q header, containing value: %q", "Content-Type", JSONAPIContentType)) |
| 39 | return false |
| 40 | } |
| 41 | |
| 42 | // parse request |
| 43 | err := jsonapi.UnmarshalPayload(r.Body, data) |
| 44 | if err != nil { |
| 45 | WriteError(w, http.StatusUnprocessableEntity, |
| 46 | fmt.Errorf("can't parse content: %v", err)) |
| 47 | return false |
| 48 | } |
| 49 | |
| 50 | // validate request |
| 51 | return ValidateRequest(w, r, data) |
| 52 | } |
| 53 | |
| 54 | // UnmarshalMany processes the request content that has an array of objects and fills passed data struct with the |
| 55 | // correct jsonapi content. After un-marshaling the struct will be validated with |
searching dependent graphs…