FromJSON is a token extractor. Reads a json request body and extracts the json based on the given field. The request content-type should contain the: application/json header value, otherwise this method will not try to read and consume the body.
(jsonKey string)
| 40 | // The request content-type should contain the: application/json header value, otherwise |
| 41 | // this method will not try to read and consume the body. |
| 42 | func FromJSON(jsonKey string) TokenExtractor { |
| 43 | return func(ctx *context.Context) string { |
| 44 | if ctx.GetContentTypeRequested() != context.ContentJSONHeaderValue { |
| 45 | return "" |
| 46 | } |
| 47 | |
| 48 | var m context.Map |
| 49 | ctx.RecordRequestBody(true) |
| 50 | defer ctx.RecordRequestBody(false) |
| 51 | if err := ctx.ReadJSON(&m); err != nil { |
| 52 | return "" |
| 53 | } |
| 54 | |
| 55 | if m == nil { |
| 56 | return "" |
| 57 | } |
| 58 | |
| 59 | v, ok := m[jsonKey] |
| 60 | if !ok { |
| 61 | return "" |
| 62 | } |
| 63 | |
| 64 | tok, ok := v.(string) |
| 65 | if !ok { |
| 66 | return "" |
| 67 | } |
| 68 | |
| 69 | return tok |
| 70 | } |
| 71 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…