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

Merge branch '42-unescape-values-when-splitting-form-data-into-key-value-pairs' into 'main'

Resolve "Unescape values when splitting form data into key-value pairs"

See merge request !55
parents b085bd27 8bd421ec
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,9 +29,17 @@ func FormToKeyValuePairs(body string) []KeyValuePair {
}
var value string
var err error
if len(split) > 1 {
if doUnescapeValues {
value, err = url.QueryUnescape(split[1])
if err != nil {
return nil, err
}
} else {
value = split[1]
}
}
kv := KeyValuePair{
Key: key,
......@@ -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.
Please register or to comment