Skip to content
Snippets Groups Projects
Commit 8bd421ec authored by James Page's avatar James Page
Browse files

#42 Unescape values when splitting form data to key-value pairs

parent b085bd27
No related branches found
No related tags found
1 merge request!55Resolve "Unescape values when splitting form data into key-value pairs"
......@@ -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.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment