diff --git a/struct_utils/struct_utils.go b/struct_utils/struct_utils.go index 30a16412d1095447140ee62eab055aaac8751c21..21e562fb5777b2cf3afaaf7f9fd8b7707e66050e 100644 --- a/struct_utils/struct_utils.go +++ b/struct_utils/struct_utils.go @@ -3,6 +3,7 @@ package struct_utils import ( "github.com/samber/lo" "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors" + "net/url" "reflect" "strings" ) @@ -15,7 +16,8 @@ type KeyValuePair struct { // FormToKeyValuePairs returns a string-based map of strings as derived from posted form keys and values. // e.g. oauth_consumer_key=mlhgs&oauth_consumer_secret=x240ar&oauth_verifier=b0qjbx&store_base_url=http%3A%2F%2Flocalhost.com%2Fstore -func FormToKeyValuePairs(body string) []KeyValuePair { +// If values are URL encoded, set doUnescapeValues to true. +func FormToKeyValuePairs(body string, doUnescapeValues bool) ([]KeyValuePair, error) { out := []KeyValuePair{} parts := strings.Split(body, "&") for _, p := range parts { @@ -27,8 +29,16 @@ func FormToKeyValuePairs(body string) []KeyValuePair { } var value string + var err error if len(split) > 1 { - value = split[1] + if doUnescapeValues { + value, err = url.QueryUnescape(split[1]) + if err != nil { + return nil, err + } + } else { + value = split[1] + } } kv := KeyValuePair{ @@ -38,7 +48,7 @@ func FormToKeyValuePairs(body string) []KeyValuePair { out = append(out, kv) } - return out + return out, nil } // GetValue returns the value for the given key from a KeyValuePair slice.